Click here to Skip to main content
15,898,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using the following code to allocate the memory for char** dbNames.
There are a total count no. of names
dbNames = new char*[count];
for(int i=0;i<count;i++)
{
  fscanf(imgListFile,"%s";,imgFilename);
  dbNames[i]=new char[strlen(imgFilename+1)];
  strcpy(dbNames[i],imgFilename);
}

It works fine but at the time of deleting I am using following code
where count and gCount are the same.
for(int i=0;i<gCount;i++)
{
  if(dbNames[i])
  {
    delete [] dbNames[i];
    dbNames[i] = NULL;
  }
}

But it throws the exception and gives the warning that heap will be corrupt.
Can anybody suggest me the proper solution to free the memory?

Thanks & Regards
Ramkrishna
Posted
Updated 3-Feb-10 1:43am
v3

Just an advice - use std::vector<std::string> instead of char** and forget about your troubles. ;)
 
Share this answer
 
Have a look at the line
ramkrishna.jangale wrote:
dbNames[i]=new char[strlen(imgFilename+1)];

Try to change it to
MIDL
dbNames[i]=new char[strlen(imgFilename) + 1];


If imgFilename is a char * you move the pointer one char to the end and the result of strlen() is 1 byte less than the string length. So all allocated memory blocks for the filenames are two bytes too small, and not as needed one byte bigger for the tailing zero.
 
Share this answer
 
v2
Since you have only posted the allocation and deletion code, one can only assume that you have somehow corrupted one of these pointers elsewhere. Try stepping through the code with the debugger to see whether your pointers are still valid.
 
Share this answer
 
Are you sure that count and gCount are the same? And why don't you use actually the same variable?
As Richard already noted, may be you did a mistake in the 'intermediate code'.
What is the exception thrown? At what value of index i it happens (these are all useful info for you...).

BTW Why don't you eventually delete dbNames too?
:)
 
Share this answer
 
Edit: stebich is right. I looked right past that definite bug.



Heap corruption and memory leak are not the same thing. Detection of heap corruption is after the cause. So all that you know is that you have done something bad sometime before you get the exception. This could be a nasty one to find and it is probably not in the code you posted.

In the code you posted, I only see one point that might cause a heap corruption. You are using a variable imgFilename in your call to fscanf, but you haven't indicated what this is. Presumably it is a pointer to a char buffer. If it is dynamically allocated and is not long enough, fscanf will write past the buffer. This would be exactly the kind of problem you are looking for. (Even if it is not dynamically allocated, it is still a problem if it is not big enough, just a different one.)

Edit: If gCount is bigger than count originally was, that could also produce your exception.

Not this problem, but don't forget that you will also have to delete the memory allocated for dbNames.

As a side note, your code will probably leak memory in the presence of exceptions. (Your code is almost entirely C and C doesn't have exceptions, but as you are compiling as C++, you are working in an environment with exceptions.) One way to deal with this is to use streams and std::string instead of fscanf and char arrays. Another is to use smart pointers. A third is to use a rat's nest of try - catch blocks that will be difficult to get right, difficult to maintain, and make it difficult to figure out what your code is doing.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900