Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why does this crash?

Reply
Thread Tools

why does this crash?

 
 
Mr Fish
Guest
Posts: n/a
 
      10-10-2005
I don't understand why this crashes in the dtor of Info.

Can someone explain to me what I'm missing?

Thanks

//---------------------------------------------------------
struct Info
{
int* m_pData;

Info():m_pData(0){}
Info(int* pData):m_pData(pData)
{}

~Info(){if (m_pData) delete m_pData;}
};

class Base
{
protected:

Info m_SomeInfo;

public:

virtual ~Base(){}
};


class Derived : public Base
{
public:

Derived(){m_SomeInfo = Info(new int(5));}
};



int main()
{
Derived d;

return 0;
}

//------------------------------------------------------
 
Reply With Quote
 
 
 
 
PasalicZaharije
Guest
Posts: n/a
 
      10-10-2005
In class Derived at line:

m_SomeInfo = Info(new int(5));

is error. With this, m_SomeInfo.m_pData will point on same location like
temporary created with Info(new int(5)). So:

Info(new int(5)).m_pData is same pointer as
m_SomeInfo.m_pData.

This will not crach your programm but after destructor is called
everithing is down. First is called destructor for Info(new int(5)) in
ctor Derived(), than in main. Both destructors will try to erase same
memory.

To prevent this, make operator = in class Info:

struct Info
{
int* m_pData;

Info():m_pData(0){}
Info(int* pData):m_pData(pData) {}

Info& operator=(const Info&) {
if (this != &Info) { // we do not need self assignment
// allocate m_pData an copy
// Info.m_pData in it
}
return *this;
}

~Info(){if (m_pData) delete m_pData;}
};

Probably, you will need and copy ctor like:

Info(Info &i) {
// allocate and copy
}



Best,Zaharije Pasalic



 
Reply With Quote
 
 
 
 
Mr Fish
Guest
Posts: n/a
 
      10-10-2005

Argh! Thankyou! )


On Mon, 10 Oct 2005 08:29:57 -0400, "PasalicZaharije"
<(E-Mail Removed)> wrote:

>In class Derived at line:
>
> m_SomeInfo = Info(new int(5));
>
>is error. With this, m_SomeInfo.m_pData will point on same location like
>temporary created with Info(new int(5)). So:
>
>Info(new int(5)).m_pData is same pointer as
>m_SomeInfo.m_pData.
>
>This will not crach your programm but after destructor is called
>everithing is down. First is called destructor for Info(new int(5)) in
>ctor Derived(), than in main. Both destructors will try to erase same
>memory.
>
>To prevent this, make operator = in class Info:
>
>struct Info
>{
> int* m_pData;
>
> Info():m_pData(0){}
> Info(int* pData):m_pData(pData) {}
>
> Info& operator=(const Info&) {
> if (this != &Info) { // we do not need self assignment
> // allocate m_pData an copy
> // Info.m_pData in it
> }
> return *this;
> }
>
> ~Info(){if (m_pData) delete m_pData;}
>};
>
>Probably, you will need and copy ctor like:
>
>Info(Info &i) {
> // allocate and copy
>}
>
>
>
>Best,Zaharije Pasalic
>
>

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-10-2005
Mr Fish wrote:
>
> I don't understand why this crashes in the dtor of Info.
>
> Can someone explain to me what I'm missing?


Rule of three:
If a class needs a custom destructor, it most likely also needs a
custom copy constructor and a custom assignment operator.

Your class Info falls into this category.

What is going on:
>
> Derived(){m_SomeInfo = Info(new int(5));}


Info( new int(5))

A single int is generated (with the value 5 in it)



o------------>+------+
| 5 |
+------+

and a pointer to it is passed in the constructor of the temporary
Info object. The constructor does:

> Info(int* pData):m_pData(pData)


i.e. stores this pointer

temporary Info object
+-----------+
| m_pData |
| o------------+
+-----------+ |
|
+----------+
|
+--->+------+
| 5 |
+------+

What's next? Then comes the assignment in

m_SomeInfo = Info(new int(5)

Since you didn't write an assignment operator on your own, the
compiler generated one for you, which does:

Info& Info:perator=( const Info& Arg )
{
m_pData = Arg.m_pData;
}

That is: a memberwise assignment of all members.
Well in your case, this boils down to:

temporary Info object
+-----------+
| m_pData |
| o------------+
+-----------+ |
|
+----------+
|
+--->+------+
+--->| 5 |
| +------+
|
+--------------+
|
Derived |
+---------------------+ |
| | |
| Base | |
| +----------------+ | |
| | m_SomeInfo | | |
| | +------------+ | | |
| | | m_pData | | | |
| | | o---------------+
| | +------------+ | |
| +----------------+ |
+---------------------+

That is: The m_pData pointer in m_SomeInfo inside Base points
to the same int as the temporary Info object does.

Well. A few nanoseconds later, the temporary info object ceases to
exist. It's destructor is called, which does:

~Info(){if (m_pData) delete m_pData;}

(The if is not necessary. You can safey delete a 0 pointer).
Thus the temporary object deletes the int it has allocted just
before itself gets wiped out of memory. This leaves you with:

+--->
|
|
+--------------+
|
Derived |
+---------------------+ |
| | |
| Base | |
| +----------------+ | |
| | m_SomeInfo | | |
| | +------------+ | | |
| | | m_pData | | | |
| | | o---------------+
| | +------------+ | |
| +----------------+ |
+---------------------+


Ooops. The pointer m_pData in m_SomeInfo now points to nowhere
in memory. So when finaly that Derived object gets destroyed, all
destructors are run, especially the dtor of m_SomeInfo which tries
to delete .... hmm ... nothing sensible. There is no data anymore
where the pointer is pointing to. -> Claboom.

Google for 'Rule of three C++'
and should find enough references on how to cure this thing.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 03-09-2011 11:00 PM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM



Advertisments