BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

delete this in destructor

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Max
Guest





PostPosted: Sun Dec 18, 2005 12:54 am    Post subject: delete this in destructor Reply with quote



I vaguely remember a discussion about this, but can't find it back. Can
I deallocate the memory of a class by having "delete this" in the
destructor? Eg:

class EG
{
private:
int somevariable;
public:
EG(int i){somevariable=i;}
~EG(){delete this;}
}

void function(void)
{
EG eg = new EG(2);
....
}

Will the allocated memory get freed when the function terminates or will
there go something very wrong when this happens?

Regards,

--
Max

C++U2
Back to top
Rob
Guest





PostPosted: Sun Dec 18, 2005 2:24 am    Post subject: Re: delete this in destructor Reply with quote



Max wrote:

Quote:
I vaguely remember a discussion about this, but can't find it back.
Can I deallocate the memory of a class by having "delete this" in the
destructor? Eg:

class EG
{
private:
int somevariable;
public:
EG(int i){somevariable=i;}
~EG(){delete this;}
}

void function(void)
{
EG eg = new EG(2);
....
}

Will the allocated memory get freed when the function terminates or
will there go something very wrong when this happens?


The memory will not be freed when the function terminates, and
something very wrong will probably happen when (or if) the object is
eventually destroyed.

The destructor is invoked in the process of destroying an object (and
deleting an object is one way to destroy it). Placing "delete this;"
into the destructor effectively attempts to destroy the object while
already in the process of destroying it, and results in undefined
behaviour.

Also, within function, eg should be declared as a pointer.

To achieve effective cleanup, three common approaches are;

class EG
{
private:
int somevariable;
public:
EG(int i){somevariable=i;}
~EG(){}
}

void function(void)
{
EG eg(2);
....
// eg will be destroyed as the function returns
}

void AlternateFunction()
{
EG *eg = new EG(2);

delete eg;
}

#include <memory>

void SaferAlternateFunction()
{
std::auto_ptr<EG> eg(new EG(2));
...
}

The first and third forms are safe, as they implicitly destroy the
object. In the first form, the destructor is automatically invoked.
In the third form, the std::auto_ptr<EG> object is invoked as the
function returned, and it's destructor destroys the object created with
operator new.

The second form (with a manual deletion) is unsafer: both because of
potential to forget to delete the object before returning, and because
it takes more effort to ensure the object is destroyed if an exception
is thrown.


Back to top
Max
Guest





PostPosted: Sun Dec 18, 2005 5:04 pm    Post subject: Re: delete this in destructor Reply with quote



Thanks for your answers Rob.

Rob wrote:

Quote:

The memory will not be freed when the function terminates, and
something very wrong will probably happen when (or if) the object is
eventually destroyed.

The destructor is invoked in the process of destroying an object (and
deleting an object is one way to destroy it). Placing "delete this;"
into the destructor effectively attempts to destroy the object while
already in the process of destroying it, and results in undefined
behaviour.

[...]


I was afraid for that.
What I was trying to do is open a file in the constructor and close it
in the destructor. This works fine if you only open the file once and
let it close when you leave the scope of the object. But when the file
opening happens in a loop I only can make it work when I use the
new/delete aproach of the object. And as you stated, that mechanism is
less save. So for that reason I hoped that the destructor could free the
memory, although it was his own memory space too. I understand this will
not work, what is a pitty, because I don't like the try / catch and
finally constructs around the file handling in my functions.

Thanks,

--
Max

C++U2

Back to top
Alan Bellingham
Guest





PostPosted: Sun Dec 18, 2005 5:33 pm    Post subject: Re: delete this in destructor Reply with quote

Max <nomail (AT) 4me (DOT) nl> wrote:

Quote:
I was afraid for that.

Why?

Quote:
What I was trying to do is open a file in the constructor and close it
in the destructor. This works fine if you only open the file once and
let it close when you leave the scope of the object.

So, something like:

{
MyFile f("C:\temptest"); // open

} // close

Quote:
But when the file
opening happens in a loop I only can make it work when I use the
new/delete aproach of the object.

What? What's wrong with

for (int i = 0; i != 10; ++i)
{
MyFile f("C:\temptest"); // open

} // close

Quote:
not work, what is a pitty, because I don't like the try / catch and
finally constructs around the file handling in my functions.

If you think you need try/catch/finally, then you're going the wrong
way.

Oh, and in


MyFile* f = new MyFile("");
...
delete f;

the delete statement does _TWO_ things. It calls the objects destructor,
and then it releases the memory.

Alan Bellingham
--
Team Thai Kingdom
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html> netiquette

Back to top
Max
Guest





PostPosted: Sun Dec 18, 2005 6:15 pm    Post subject: Re: delete this in destructor Reply with quote

Alan Bellingham wrote:
[...]
Quote:
What? What's wrong with

for (int i = 0; i != 10; ++i)
{
MyFile f("C:\temptest"); // open

} // close


Thanks Alan, I just didn't realize the file would close each loop and I

thought a new instance of the file object would be created, using up
more and more memory.

[...]

Quote:

If you think you need try/catch/finally, then you're going the wrong
way.

Oh, and in


MyFile* f = new MyFile("");
...
delete f;

the delete statement does _TWO_ things. It calls the objects destructor,
and then it releases the memory.

Alan Bellingham

Best regards,

--
Max

C++U2

Back to top
Alan Bellingham
Guest





PostPosted: Sun Dec 18, 2005 7:39 pm    Post subject: Re: delete this in destructor Reply with quote

Max <nomail (AT) 4me (DOT) nl> wrote:

Quote:
Thanks Alan, I just didn't realize the file would close each loop and I
thought a new instance of the file object would be created, using up
more and more memory.

Ah, I thought you were working under some form of misconception. Glad to
have cleared it up.

Alan Bellingham
--
Team Thai Kingdom
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html> netiquette

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Dec 19, 2005 6:52 pm    Post subject: Re: delete this in destructor Reply with quote


"Max" <nomail (AT) 4me (DOT) nl> wrote


Quote:
Can I deallocate the memory of a class by having "delete this"
in the destructor?

No. That would be very dangerous to do. The class is already in a state of
being destroyed. There is no need to 'delete' it again.


Gambit



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.