new/delete in ctor/dtor

Started by
16 comments, last by MichaelBartman 20 years, 8 months ago
Hey, I have two classes similar to the two examples below.

class b
{

};

class a
{
     b *temp;
     a *next, *prev;
public:
     a()
     {
          next = 0;
          prev = 0;
          temp = new b();
     }
     ~a()
     {
          delete temp;
     }
};
When I make an instance of class a, and then delete it it will crash with an exception error in Release mode. But in debug mode, it runs flawlessly as far as I can tell. If I take out b *temp and delete temp from the ctor/dtor then it does not crash. I thought that maybe it was because b was an empty class(it''s a dummy class that I haven''t filled in yet), but even if I fill in b with member variables and methods it still crashes. Any suggestions would be warmly welcomed. Thanks, Michael Bartman
Michael BartmanCEO, Lead ProgrammerDark Omen Studios
Advertisement
Define a copy constructor/assignment operator to do a deep copy.
This might sound a little naive, but what is a deep copy and how would I implement it using my example?

Thanks
Michael BartmanCEO, Lead ProgrammerDark Omen Studios
quote:Original post by MichaelBartman
This might sound a little naive, but what is a deep copy and how would I implement it using my example?

Thanks


That''s just where you make a complete copy of an object and all its members (and their members, and so on), and not just copy the pointers.

Usually you''d do this in a copy constructor (a constructor that takes a reference to an object of its own class, and just gives you a copy of that object).
The code compiles fine on my machine... but The problem may be this. You explicitly specify the default constructor in the new statement, by your empty parenthesis. You have not explicity defined such a function, so maybe your compiler (in release mode) does not allow access to its internal default functions in classes it generates (such as the default constructor). In other words... if you put:

temp = new b;
instead of
temp = new b();

it might work. As I said, it compiles on mine in any case.

[edited by - dmounty on August 10, 2003 6:03:28 PM]
^^ No.

I''ll bet that the problem is that the OP passes this class by value somewhere.

OP, consider:
a fst; // creates an a
void func(a A); // function that takes an a
func(fst); // passes by value, ie sends a copy of fst
// function returns, the destructor is called for the copy and the memory fst and the copy points TO is deleted
} // end of main(), deconstructor called on fst, which tries to delete already deleted memory, ERROR
why would this only happen with a release compile, and not with a debug compile? heh, I forgot he might actually have a program attatched to the class. I was just thinking about instantiating it... then exitting.
OP: If you want information on deep copies (and general object construction and initialisation), have a search on Google (or better yet, purchase an introductory C++ book). Hopefully, you should end up with something like so:
class b{...}; class a{public:    a() : next(0), prev(0), temp( new b() ) {}    a( const a& rhs ) : next( rhs.next ), prev( rhs.prev ), temp( new b( *rhs.temp ) ) {}    a& operator=( const a& rhs );    ...private:    b* temp;    a* next;    a* prev;    ...}; a& a::operator=( const a& rhs ){    if ( &rhs != this )    {        next = rhs.next;        prev = rhs.prev;        delete temp;        temp = new b( *rhs.temp );    }     return *this;}


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on August 10, 2003 6:29:51 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
quote:Original post by dmounty
why would this only happen with a release compile, and not with a debug compile?


Because the release compile optimizes something which exposes the bug in a way where its not just an unchecked error code someplace, but a crash bug...

Never happened to you before? Lucky you...

It could be that there is no explicit CTOR/DTOR for class b. I would have thought the default ones would suffice, but the delete operator handles things differently than if a declared variable is freed due to going out of a local scope. I've never seen this problem myself, so I don't know for sure, but thats what I'd try.

[EDIT] Not sure if this would sove the mystery of why it works in debug mode. But it could very well be that the delete operator works differently under debug and release modes.

Ravyne, NYN Interactive Entertainment
[My Site][My School][My Group]

[edited by - ravyne on August 10, 2003 7:51:33 PM]

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement