Archived

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

MichaelBartman

new/delete in ctor/dtor

Recommended Posts

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
^^ 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Thanks for all of your help guys,

I had tried doing temp = new b; but that it still had the same problem. That''s when I tried filling in a ctor and dtor for class b, btu that still didn''t work.

Deep copy just sounds more confusing than it really is . I had a quick look in my copy of C++ Primer 2nd Edition, but there wasn''t anything about it so I figured it was something complex :|

Thanks again for all of the help!

Share this post


Link to post
Share on other sites
In class a, define this:

a(const a&)
{
temp = new b;
}
a& operator =(const a& rhs)
{
if (this == &rhs) // not really necessary in this example, but it''s a good habit to always check for self-assignment

return *this;
delete temp;
temp = new b;
return *this;
}

Share this post


Link to post
Share on other sites
fallenang3l''s code also pointed out something else to watch for: If you''re implementing a copy constructor, you''ll also want to implement an assignment operator, because that''s another source of initialization/finalization. It''s the Rule of Three (which I seem to be quoting almost daily these days): If your class specifies a destructor, an assignment operator, or a copy constructor, it will most likely need all three.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
I don''t know how it could be any code that I omitted, because there is only a problem when I do a new on an object of type b, and then delete it. I don''t do anything to it besides that, and when I comment out the line with the new and the line with the delete everything works fine and dandy.

Maybe Visual Studio is just telling me I should have stuck with 6 and not gone to .NET? lol

Share this post


Link to post
Share on other sites
quote:
Original post by MichaelBartman
Maybe Visual Studio is just telling me I should have stuck with 6 and not gone to .NET? lol


...hardly. Implement what the other posters have already mentioned and you should be fine. If defining a proper copy ctor, dtor, and assignment operator doesn''t fix the problem, then your problem lies elsewhere.

Share this post


Link to post
Share on other sites
quote:
Original post by C-Junkie
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...


Its happened to me and the ensuing language spewing from my mouth made the wallpaper peel.

Share this post


Link to post
Share on other sites