Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

DanG

Can an object delete itself?

This topic is 5799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m working on a class that is referance counted. Whenever something wants to referance the object they call object->AddRef(); and when done with it they call object->ReleaseRef(); The thing is that the way it is setup now, I have to make the last person holding a referance to delete it. The code becomes like this: if (object->ReleaseRef()) // returns true if this is the last ref { delete object; } A little intricate and convoluted. I want ReleaseRef to delete itself if there are no more referances, but I don''t know how to do this. Anyone have ideas?

Share this post


Link to post
Share on other sites
Advertisement
Yes, an object can delete itself like so...

void Object::ReleaseRef()
{
m_refCount--;
if (m_refCount == 0)
delete this;
}

I''ve seen this technique employed in a number of COM examples. Just make sure you don''t continue to use the object after it has deleted itself.

Share this post


Link to post
Share on other sites
There''s already a thread on this subject in the forum archive, and there''s a detailed response in the C++ FAQ Lite(make a search for ''delete this'').

In a nutshell, it''s OK, but you have to be careful about it. See the link for details.

Cédric

Share this post


Link to post
Share on other sites
If you''re going to delete this, you have to be sure that the object was allocated with new. Something like this should work:

class foo {
public:
  static foo *create() { return new foo; }
private:
  foo() { }
};

Share this post


Link to post
Share on other sites
If you're worried about it not being created with new, then you can free() it instead of deleting it, and also call the destructor just before you free it.


void foo::Release()
{
if( m_RefCount == 1 )
{
this->~foo();
free(this);
}
else
m_RefCount --;
}


I like it better than Beer Hunter's suggestion, because if you can't trust it to be created with new, how can you trust it to be created with foo::create()?

~CGameProgrammer( );

[edited by - CGameProgrammer on August 30, 2002 2:43:27 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by CGameProgrammer
If you''re worried about it not being created with new, then you can free() it instead of deleting it, and also call the destructor just before you free it.


void foo::Release()
{
if( m_RefCount == 1 )
{
this->~foo();
free(this);
}
else
m_RefCount --;
}


I like it better than Beer Hunter''s suggestion, because if you can''t trust it to be created with new, how can you trust it to be created with foo::create()?

~CGameProgrammer( );



That''s why:
private:
foo() { }

It''s private. The only thing that can create the object is itself.

Joakim Asplund
http://megajocke.cjb.net

Share this post


Link to post
Share on other sites
quote:
Original post by CGameProgrammer
If you''re worried about it not being created with new, then you can free() it instead of deleting it, and also call the destructor just before you free it.


void foo::Release()
{
if( m_RefCount == 1 )
{
this->~foo();
free(this);
}
else
m_RefCount --;
}


I like it better than Beer Hunter''s suggestion, because if you can''t trust it to be created with new, how can you trust it to be created with foo::create()?

~CGameProgrammer( );

[edited by - CGameProgrammer on August 30, 2002 2:43:27 PM]


In Beer Hunter''s example the object can only be created by the static create method because the constructor is private (and can only be called from inside the class)

Share this post


Link to post
Share on other sites
quote:
Original post by CGameProgrammer
If you''re worried about it not being created with new, then you can free() it instead of deleting it

You must not free() something that was not allocated using malloc().

Share this post


Link to post
Share on other sites
quote:
Original post by CGameProgrammer
If you''re worried about it not being created with new, then you can free() it instead of deleting it, and also call the destructor just before you free it.


free() will blow up in exactly the same way as delete if the object is allocated on the stack. Nice try, no cigar.


"When you know the LORD you have no need for masturbation!"

Share this post


Link to post
Share on other sites
quote:
Original post by megajocke
It's private. The only thing that can create the object is itself.




Just for shits and giggles:


    
class foo
{
public:
~foo(){}
static foo* create() { foo* obj = new foo(); obj->add_ref(); return obj; }

size_t add_ref()
{
return ++m_refcount;
}
size_t release()
{
if(!--m_refcount)
{
delete this;
}
return m_refcount;
}

private:
foo(){}
size_t m_refcount;
};

void main()
{
foo* f = reinterpret_cast<foo*>(::malloc(sizeof(foo)));
// oh shit!

f->release(); // wtf

}


Yes, I know. I cheated.

Also... if you want to pass around a reference counted pointer to an object, look into boost::shared_ptr

[edited by - daerid on August 30, 2002 4:54:20 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!