Is this safe?

In a class Destructor, I call a method in another class like DoSomething( self ). In DoSomething, the code looks something like this:

if someCondition then
  AddToArray thatDestructingObject
end if

Is it safe to store a reference to, and reuse, an object after its Destructor has fired? My testing hasn’t shown a problem, but I’d like to make sure.

Yes, you can create self-resurrecting objects. I wouldn’t generally call it a good idea, but it does have its uses.

wouldn’t that lead into destructor being called again at a later time?

Yes, and that works. And I compensated for it.

Thanks Joe.

This topic can be taken public now.

Hey, I could move it myself! Who knew?

This has me very curious: Can you work up a quick hypothetical where this would be useful? I’m genuinely curious why you would want to do this rather than just not kill off an object until you are sure you don’t need it any longer.

Sure. I have a master class that hands off an object to a caller. When the caller is done with it, that object goes out of scope and, in the Destructor, calls MasterClass.Release( self ). Depending on conditions, the master class may decide to let it go or store it to hand off again later to another caller.

I could depend on the caller to call Release( object ) manually when it’s done with it, but this way makes it automagic and won’t fail even in the case of an exception.

Slick.