Sending an immutable object to a thread
Frank Pagliughi via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jul 22 10:17:14 PDT 2015
On Wednesday, 22 July 2015 at 09:04:49 UTC, Marc Schütz wrote:
>
> But as long as the original pointer is still on the stack, that
> one _will_ keep the object alive. It is only a problem if all
> pointers to a GC managed object are stored in places the GC
> isn't informed about.
Sorry, I have gotten confused. In Ali's example, the pointer to a
class object (via the address-of '&' operator) actually points
into the GC heap. It is *not* a pointer to a pointer, right?
My reading of the Garbage web doc page is that this pointer to
memory in the GC heap is sufficient (by some magic) to keep the
memory alive, in and of itself.
So the pointer, passed to the other thread is sufficient to keep
the memory alive, even if the original reference disappears.
Or, to put it another way, getting threads out of the equation,
is this safe?
class MyThing { ... }
MyThing* create_a_thing() {
MyThing mt = new MyThing();
do_something_with(mt);
return &mt;
}
void main() {
MyThing* pmt = create_a_thing();
// ...
}
The "thing" will remain alive for the duration of main() ??
Thanks
More information about the Digitalmars-d-learn
mailing list