digitalmars.D.learn - new returning the same memory....
- Codifies (15/15) Nov 08 2018 when creating a new instance of a class
- Codifies (2/17) Nov 08 2018 I'll try to spin up a minimal example...
- drug (10/35) Nov 08 2018 You take address of local variable, not a class instance. Just use the
- Daniel Kozak (8/32) Nov 08 2018 Did you try disable gc?
when creating a new instance of a class aclass a = new aclass(); I was under the impression that this created a new chunk of memory on the heap... however I'm trying to create this class instance in another classes method, I also need to store a pointer to this newly created instance in the same method. when I look at the actual value of the pointer, although it does change after multiple goes, frequently its the same value. It almost looks like its allocating on the local stack and its made a new instance thats entirely local to the factory method. do I need to manually allocate the memory ?? how do I do this? how can I allow the GC to clean this up? I'm *really* discombobulated now, as new doesn't seem to be working how I thought it did!!!
Nov 08 2018
On Thursday, 8 November 2018 at 11:46:44 UTC, Codifies wrote:when creating a new instance of a class aclass a = new aclass(); I was under the impression that this created a new chunk of memory on the heap... however I'm trying to create this class instance in another classes method, I also need to store a pointer to this newly created instance in the same method. when I look at the actual value of the pointer, although it does change after multiple goes, frequently its the same value. It almost looks like its allocating on the local stack and its made a new instance thats entirely local to the factory method. do I need to manually allocate the memory ?? how do I do this? how can I allow the GC to clean this up? I'm *really* discombobulated now, as new doesn't seem to be working how I thought it did!!!I'll try to spin up a minimal example...
Nov 08 2018
08.11.2018 14:48, Codifies пишет:On Thursday, 8 November 2018 at 11:46:44 UTC, Codifies wrote:You take address of local variable, not a class instance. Just use the var, not its address: ``` auto a = new MyClass(); writefln("%s", &a); // here you write to console address of local var a, not the class instance writefln("%s", cast(void*)a); // here you write to console address of the class instance ```when creating a new instance of a class aclass a = new aclass(); I was under the impression that this created a new chunk of memory on the heap... however I'm trying to create this class instance in another classes method, I also need to store a pointer to this newly created instance in the same method. when I look at the actual value of the pointer, although it does change after multiple goes, frequently its the same value. It almost looks like its allocating on the local stack and its made a new instance thats entirely local to the factory method. do I need to manually allocate the memory ?? how do I do this? how can I allow the GC to clean this up? I'm *really* discombobulated now, as new doesn't seem to be working how I thought it did!!!I'll try to spin up a minimal example...
Nov 08 2018
Did you try disable gc? import core.memory : GC; GC.disable; aclass a = new aclass(); I believe that your aclass go out of scope and there is no active reference to this so GC can collected it and reuse its memory On Thu, Nov 8, 2018 at 12:50 PM Codifies via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:On Thursday, 8 November 2018 at 11:46:44 UTC, Codifies wrote:when creating a new instance of a class aclass a = new aclass(); I was under the impression that this created a new chunk of memory on the heap... however I'm trying to create this class instance in another classes method, I also need to store a pointer to this newly created instance in the same method. when I look at the actual value of the pointer, although it does change after multiple goes, frequently its the same value. It almost looks like its allocating on the local stack and its made a new instance thats entirely local to the factory method. do I need to manually allocate the memory ?? how do I do this? how can I allow the GC to clean this up? I'm *really* discombobulated now, as new doesn't seem to be working how I thought it did!!!I'll try to spin up a minimal example...
Nov 08 2018