Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory leak during Exception handling ???

Status
Not open for further replies.

deyzel

Programmer
Apr 4, 2001
15
CA
Hi All experts,

I am using a try/catch block to catch exceptions during a SQL insert operation. I want to prevent duplicates from being placed in the database and when the exception occurs the application should just continue. That part work fine but I am leaking memory.

If I "throw" in the catch block, no memory is lost, but if I only continue without calling "throw", I have 12 memory leaks ranging from 16 to 388 bytes each. I do not want to show a message everytime the DB prevents me from inserting a duplicate.

How should I handle the Exception without showing anything to the user and without leaking memory.

Thanks
deyzel
 
The try block is correct but it sounds like you are creating an object that isn't being deleted in an exception. In this case, you need to use a try . . .__finally block. For example, I use this code when I read from my registry.
Code:
TRegistry* regGet = new TRegistry; // Create reg object
try
{
    regGet->RootKey = HKEY_LOCAL_MACHINE;
    //Get the keys and their values

}
__finally
{
    // This executes if there is an exception or not
    delete regGet; // Delete reg object
};

What I have not tried is to use a try ... catch ... __finally block. NOTE: There can only be one __finally block for a try block.

James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top