Error on delete [] v  
Author Message
bookysmell2004





PostPosted: Visual C++ Language, Error on delete [] v Top

Hi,

I have a problem with a class. The declator is

class Configuration {

public:

struct conf_table {

char name[40];

char value[10];

};

struct conf_table *conftable;

int size; //rows on conftable

int max_size; //maximum rows on conftable

public:

Configuration(void); //Constructor

~Configuration(void); //Destructor

};

And the code is

Configuration::Configuration(void) //Constructor

{

size=0;

max_size=256;

conftable = new conf_table[max_size]; //initialize conftable

}

Configuration::~Configuration(void) //Destructor

{

delete [] conftable;

}

The problem is that i get an error message (Heap Corruption) after Destructor execution. Can someone help me Is there a problem deleting a pointer to a structure Thanks in advance.



Visual C++3  
 
 
Marius Bancila





PostPosted: Visual C++ Language, Error on delete [] v Top

well, most likely you are writing into conftable beyond the bounds. Make sure the last element you access is conftable[255].

Also notice this is not necessary in C++, that is purely C:

struct conf_table *conftable;

This is enough:

conf_table *conftable;

However, I suggest going for the C++ approach:

class Configuration {

public:

struct conf_table {

std::string name;

std::string value;

};

std::vector<conf_table> conftable;

public:

Configuration(void); //Constructor

~Configuration(void); //Destructor

};

And of course, you can provide method like addind, removing, accessing the elements of conftable. This way you don't have to deal with dynamically memory allocation, the STL container takes care of all.



 
 
kuphryn





PostPosted: Visual C++ Language, Error on delete [] v Top

initialize conftable null

on destructor deallocate if no null

result

Kuphryn


 
 
bookysmell2004





PostPosted: Visual C++ Language, Error on delete [] v Top

In fact this is a part of the code. There are also methods for Adding elements to the conftable. But also without them i get the same error message. I mean, there is no wrong, deleting a pointer to a structure Also, i access only one element on conftable or two. Anyway, I'll try the c++ approach to see if a get error message again. Thx a lot for the advice.
PS. Isn't it weird This small piece of code returning error

 
 
bookysmell2004





PostPosted: Visual C++ Language, Error on delete [] v Top

"initialize conftable null"

How am i supposed to do this Can you plz give an example


 
 
bookysmell2004





PostPosted: Visual C++ Language, Error on delete [] v Top

Ok. Think I found sth.
When I alter to this

Configuration::Configuration(void) //Constructor
{
size=0;
max_size=256;
conftable = 0;
}

i dont get the error message on delete! But the problem now is that i cant use conftable in a function (exception), possibly because of not using new.
Of course the program runs without problem when i dont use delete [], but i think thats not that good ... Dont know why.

Is the situation serious It looks weird ...

 
 
nobugz





PostPosted: Visual C++ Language, Error on delete [] v Top

There's nothing wrong with your original code. The trouble with heap damage is that it is almost always reported well after the damage is done. Most typically, it is a block BEFORE the one that got reported as damaged that was destroyed. Troubleshooting this can be difficult. Luckily, the "Debug memory allocator" can help you out. Start reading here if you use MFC or here if you don't. Good luck!


 
 
bookysmell2004





PostPosted: Visual C++ Language, Error on delete [] v Top

Thx a lot nobugz. _crtDbgFlag. Yes. This is the problem. I think i found sth to begin with...
Thx again everyone.

 
 
Marius Bancila





PostPosted: Visual C++ Language, Error on delete [] v Top

I suggest you read my first post again.

 
 
bookysmell2004





PostPosted: Visual C++ Language, Error on delete [] v Top

Thx Marius Bancila. I did read it and altered my code, but still had problem... Maybe i do something wrong. I dont know... I'll try again.

 
 
Marius Bancila





PostPosted: Visual C++ Language, Error on delete [] v Top

Well, I was rather refering to this part

"well, most likely you are writing into conftable beyond the bounds. Make sure the last element you access is conftable[255]."



 
 
bookysmell2004





PostPosted: Visual C++ Language, Error on delete [] v Top

Marius Bancila,
That's sounds stupid, but i'm not sure if I'm writing beyond the bounds.!!!
To check if this is the problem, i just deleted all the functions-members of the class, so no data are passed to conftable. With debugging i realized that when the destructor is called conftable is a no-null value and points to empty space. And still having the same problem. without even writing a single line of code (eg Configuration MyConfiguration; MyConfiguration.~Configuration();). Is this explainable I'm out of my bounds :) !!

 
 
Marius Bancila





PostPosted: Visual C++ Language, Error on delete [] v Top

You can call me Marius...

Now, the piece of code you posted is ok. The problem isn't there. But I guess it's just a snippet of the whole code, so we are missing something important. Unless you provide the full code, it's hard (if not impossible to spot the problem).

BTW, this is totally wrong, and definitely creates a crash...

Configuration MyConfiguration;

MyConfiguration.~Configuration();

Never do that, call the desctructor of the object. It is automatically called when the object does out of scope, or when delete is called.

What happens is that you delete conftable by calling the destructor, but you don't set it to NULL, so it's a dangling pointer, and when when the object goes out of scope and the desctructor is automatically called (a second time), it attempts to delete something that was already deleted.

Doing this removes the crash, but it's totally wrong:

Configuration::~Configuration(void) //Destructor
{
delete [] conftable;
conftable = NULL;
}

As additional reading, I recommend this article: http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c9535/



 
 
bookysmell2004





PostPosted: Visual C++ Language, Error on delete [] v Top

Ok, Marius.
I have read your article. In fact i understood liitle [well, i'm not a CodeGuru :( ]. I have not problem providing the full code. But it is large and annoying (at least at me). Let me ask you something. You mean that's wrong

Configuration::~Configuration(void) //Destructor
{
delete [] conftable;
conftable = NULL;
}

The full code, in case someone has time to read it, is uploaded at http://utopia.duth.gr/~gs6646/source.zip.
You can also download a stable version (Is It ) at http://sourceforge.net/projects/gscalculator.
Finally thx a lot for "Never do that, call the desctructor of the object.". I think im starting to realize what the problem is
"What happens is that you delete conftable by calling the destructor, but you don't set it to NULL, so it's a dangling pointer, and when when the object goes out of scope and the desctructor is automatically called (a second time), it attempts to delete something that was already deleted."

The exception occures after a call to benchmark (Type /benchmark)

Edit: Changed to this
Configuration::~Configuration(void) //Destructor
{
conftable = NULL;
delete [] conftable;
}
and seems there is no more problem... I am not sure though but the previous exception no longer exists!!!

 
 
Brian Kramer





PostPosted: Visual C++ Language, Error on delete [] v Top

Don't do that... that will cause the array to not get deleted. (You're masking the problem at the expense of a memory leak).

You have a memory corruption somewhere. Whittle your code down as much as you can, bringing allocation and deallocation closer and closer together until you can identify the cause of the corruption.