How to free memory in try-catch blocks?
Posted
by Kra
on Stack Overflow
See other posts from Stack Overflow
or by Kra
Published on 2010-06-15T19:31:49Z
Indexed on
2010/06/15
19:42 UTC
Read the original article
Hit count: 246
Hi,
I have a simple question hopefully - how does one free memory which was allocated in the try block when the exception occurs? Consider the following code:
try
{
char *heap = new char [50];
//let exception occur here
delete[] heap;
}
catch (...)
{
cout << "Error, leaving function now";
//delete[] heap; doesn't work of course, heap is unknown to compiler
return 1;
}
How can I free memory after the heap was allocated and exception occurred before calling delete[] heap? Is there a rule not to allocate memory on heap in these try .. catch blocks?
Thanks
© Stack Overflow or respective owner