Deleting an object
Rikki Cattermole via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jan 18 05:49:32 PST 2016
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.
More information about the Digitalmars-d-learn
mailing list