Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete causing an error and app crashing

Status
Not open for further replies.

paulb567

Programmer
May 5, 2004
4
0
0
GB
Hello,

I'm fairly new to visual c++ and am currently working my way thourgh a few examples of windows programming.

Anyway, the curent example I'm putting together is a very simple drawing application (the kind of thing you find in all the learn visual c++ books).

The problem I'm facing is everytime I try and delete an object the app gives me one of those error messages where I can send a report to microsoft and the app dies.

Basically

I'm creating a new object like so

CElement element;
element = new CLine(start, end, color);
m_pTempElement = &element;

This part works fine, and the element is drawn ok.

The problem comes when I try and..

delete m_pTempElement
m_pTempElement = 0;

the program crashes on the delete line.

Any ideas? Am I doing something really stupid that should be obvious?

Thanks.

ps. Its being compiled by vc++ 6 running on windows xp if this makes any difference.
 
Youre not doing delete on what you did a new on.

You did new CLine but delete on a CElement (that you've put on the stack).

Code:
CElement element = new CLine(start, end, color);
looks fishy. The CElement = pointer to a CLine assignemnt is semantically funny. What does this assignemnt mean really?



/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
The following lines dont look right unless CElement is really a pointer.

CElement element; // not a pointer
element = new CLine(start,end,color); // should see an error
m_pTempElement = &element;

If element was declared on the stack, it will call its destructor when it goes out of scope. You should not need to delete it.

I would change the code to either:

m_pTempElement = new CLine(start,end,color);
... // do stuff

You can leave the function here and have m_pTempElement still existing. Upon destruction of your class you could execute the following code. (Dont forget to initialize m_pTempElement to NULL in the constructor.)

if(m_pTempElement)
{
delete m_pTempElement;
}

You could also delete m_pTemp element within the function but if that is the case, a pointer is not required.

OR...
Judging from the code, you may not need the pointer.

CLine myLine(start,end,color);
...// do stuff
// NO DELETION REQUIRED

If myLine needs to remain around, then the pointers come into play or member variables which would require initialization in your list.

class YourClass
{
...
protected:
CLine m_line;
}

YourClass::YourClass():m_line(defaultStart,defaultEnd,defaultColor)
{}

// With pointers

class YourClass
{
...
protected:
CLine* m_pLine;
}

YourClass::YourClass():m_pLine(NULL)
{}

YourClass::~YourClass()
{
if(m_pLine)
{
delete m_pLine;
}
}

Hope these offered some help
Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top