Keeping list of Objects

tom tom_member at pathlink.com
Thu Apr 27 22:26:14 PDT 2006


In article <e2ruoi$nv8$1 at digitaldaemon.com>, Jeremy says...
>
>I'm writing a game in D, and in a loop, I'm creating new objects and keeping
>track of them by storing their pointers:
>
>e.g. I have a linked list of pointers. In the loop, I do a "OBJECT newThing =
>new OBJECT()", and then I add the pointer &newThing to the linked list. After
>that function exits (and the scope exits), I try to access these OBJECTs by
>referencing the pointers... but I get an error. I assume the garbage collector
>is freeing that space?
>
>Anyway, I want to do this because I want a linked list of different objects
>(e.g. all the objects in my game, which might be different classes -- I can cast
>these pointers later).
>
>I'm realizing that this won't work (unless I make the newThing global, then the
>data I allocated still exists later). Anyone have a better way of accomplishing
>the same thing?
>
>

why are you using a pointer to an object? i am almost certian that is your problem. you are probably storing the address of your object reference. just use a list of references, or I think you have to cast to a pointer.



More information about the Digitalmars-d mailing list