www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Object removing own last reference.

reply strtr <strtr sp.am> writes:
void externalFunc(){}

class C
{
  ..
  int index_;
  int yay;
  void removeMe()
  {
    //remove last reference to this object
    objects[_index] = null;

    //other critical code
    memberFunc();
    externalFunc();
  }
  void memberFunc(){yay++}
}

Is there anything unsafe about his code? Like the GC removing the Object
before calling memberFunc..
Jun 24 2010
next sibling parent marksibly <blitzmunter gmail.com> writes:
Hi,

Shouldn't be as 'this' will keep the object alive as long as necessary.

Mark
Jun 24 2010
prev sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
strtr <strtr sp.am> wrote:

 void externalFunc(){}

 class C
 {
   ..
   int index_;
   int yay;
   void removeMe()
   {
     //remove last reference to this object
     objects[_index] = null;

     //other critical code
     memberFunc();
     externalFunc();
   }
   void memberFunc(){yay++}
 }

 Is there anything unsafe about his code? Like the GC removing the Object
 before calling memberFunc..

A reference to it will still be on the stack, so it should be no problem. -- Simen
Jun 25 2010
parent strtr <strtr sp.am> writes:
== Quote from Simen kjaeraas (simen.kjaras gmail.com)'s article
 strtr <strtr sp.am> wrote:
 void externalFunc(){}

 class C
 {
   ..
   int index_;
   int yay;
   void removeMe()
   {
     //remove last reference to this object
     objects[_index] = null;

     //other critical code
     memberFunc();
     externalFunc();
   }
   void memberFunc(){yay++}
 }

 Is there anything unsafe about his code? Like the GC removing the Object
 before calling memberFunc..


The "this" marksibly mentioned. Thanks peoples ;)
Jun 25 2010