Reputation: 1064
I am using Dr. Memory to debug my code. It gives me the following message:
INVALID HEAP ARGUMENT: allocated with operator new[], freed with operator delete
However, whenever I use delete[]
instead of delete
, it gives me the same error. The error occurs in the following lines:
for (int x = 0; x < width; ++x)
{
delete data[x];
}
delete [] data;
Variable data
is being allocated as follows:
data = new char*[width];
for (int i = 0; i < width; ++i)
{
data[i] = new char[1];
}
Could someone please help me out?
Upvotes: 0
Views: 2454
Reputation: 934
Since you are using c++, and not C...
Re-factor the code to use the STL and std::string to create vectors of std::strings in the program.
Use of std::string will likely eliminate the memory leak.
Upvotes: 0
Reputation: 27577
You need to match the second new[]
(data[i] = new char[1]
) too:
for (int x=0; x<width; ++x){
delete [] data[x];
}
delete [] data;
Upvotes: 0
Reputation: 8514
You currently allocate the elements with
data[i] = new char[1];
and delete them with
delete data[x];
As you allocated them with the new[]
you need to delete them with delete []
.
delete [] data[x];
Upvotes: 1