www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Deleting an object

reply Maik Klein <maikklein googlemail.com> writes:
I have also asked this question here 
https://stackoverflow.com/questions/34838742/weak-references-or-pointers

But I try to word it differently.

I have a game with GameObjects, a system manages those 
GameObjects. GameObjects can hold a pointer/reference to each 
other.

But at one point a GameObject dies and when it does it shouldn't 
be able to be accessed by other GameObjects nor should it be kept 
alive in memory.

How do I express this in D?

I have also looked at core.memory.GC

   auto foo = new Foo!int();
   auto foo2 = foo;
   GC.free(foo);

   if(foo != null)
     writeln(*foo);
   if(foo2 != null)
     writeln(*foo2);

But it seems that `GC.free` behaves like C++'s `delete` and 
doesn't actually null all the pointers.
Jan 18 2016
parent Rikki Cattermole <alphaglosined gmail.com> writes:
On 19/01/16 2:47 AM, Maik Klein wrote:
 I have also asked this question here
 https://stackoverflow.com/questions/34838742/weak-references-or-pointers

 But I try to word it differently.

 I have a game with GameObjects, a system manages those GameObjects.
 GameObjects can hold a pointer/reference to each other.

 But at one point a GameObject dies and when it does it shouldn't be able
 to be accessed by other GameObjects nor should it be kept alive in memory.

 How do I express this in D?

 I have also looked at core.memory.GC

    auto foo = new Foo!int();
    auto foo2 = foo;
    GC.free(foo);

    if(foo != null)
      writeln(*foo);
    if(foo2 != null)
      writeln(*foo2);

 But it seems that `GC.free` behaves like C++'s `delete` and doesn't
 actually null all the pointers.
Correct, in native land you can't null all the pointers. They are just integers after all.
Jan 18 2016