473,513 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to deallocate memory when handling exception

Hello Experts!

Assume foo is a method in a some class or a stand-alone function and method
push is throwing an exception of class logic_error.
Assume also that I want to handle this exception in foo if it's possible.
This will cause a problem because C++ doesn't have the finally keyword and I
can't do delete both in the exception handler and after the exception
handler that's because I have put in the throw to throw it further
to let some other method to handle it. But if I remove this throw how is it
then possible to get a
solution that will hold and i a good solution. If I just remove this throw
and an exception occurs will this cause delete s two times and resuting in
undefined behaviour.

void foo()
{
IntStack* s

try
{
s = new IntStack(20);
s->push(20);
}
catch(const logic_error& e)
{
delete s;
throw; //
}

delete s;
}

Many thanks

//Tony
Aug 13 '05 #1
3 2127
Tony Johansson wrote:
Hello Experts!

Assume foo is a method in a some class or a stand-alone function and method
push is throwing an exception of class logic_error.
Assume also that I want to handle this exception in foo if it's possible.
This will cause a problem because C++ doesn't have the finally keyword and I
can't do delete both in the exception handler and after the exception
handler that's because I have put in the throw to throw it further
to let some other method to handle it. But if I remove this throw how is it
then possible to get a
solution that will hold and i a good solution. If I just remove this throw
and an exception occurs will this cause delete s two times and resuting in
undefined behaviour.

void foo()
{
IntStack* s; Note that s is uninitialized - could contain garbage.
try
{
s = new IntStack(20); new can throw badalloc - s may not get set - still contains garbage. s->push(20);
}
catch(const logic_error& e)
{
delete s;
throw; //
}

delete s;
Trying to delete garbage. Not good 10 out of 10.

Another problem, is if push throws some other (possibly future)
exception type, you will leak s.
}


To answer the OP question, I would use the RAII idion (Resource
Aquisition Is Initialization) and remove the need to handle this type of
logic manually.

#include <list>
#include <memory>

typedef std::list<int> IntStack;
struct logic_error {};

void foo()
{
std::auto_ptr<IntStack> s;

try
{
s.reset( new IntStack(20) );
s->push_back(20);
}
catch(const logic_error& e)
{
throw; //
}

}

OK - so you don't like that ? This will work too. Although, the code
above looks alot simpler and much more intuitive.

void foo()
{
IntStack* s = 0;

try
{
try
{
s = new IntStack(20);
s->push(20);
}
catch(const logic_error& e)
{
delete s;
s = 0;
if ( cant_fix_logic )
{
throw; //
}
}
} catch (...)
{
delete s;
throw;
}

delete s;
}

Aug 13 '05 #2
Gianni Mariani wrote:


To answer the OP question, I would use the RAII idion (Resource
Aquisition Is Initialization) and remove the need to handle this type of
logic manually.

std::auto_ptr<IntStack> s;


Just to underscore the point: this is exactly what auto_ptr was designed
for.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 13 '05 #3
Gianni Mariani wrote:
Tony Johansson wrote:
void foo()
{
IntStack* s;

try
{
s = new IntStack(20);
s->push(20);
}
catch(const logic_error& e)
{
delete s;
throw; //
}

delete s;


#include <list>
#include <memory>

typedef std::list<int> IntStack;
struct logic_error {};

void foo()
{
std::auto_ptr<IntStack> s;

try
{
s.reset( new IntStack(20) );
s->push_back(20);
}
catch(const logic_error& e)
{
throw; //
}

}

OK - so you don't like that ? This will work too. Although, the code
above looks alot simpler and much more intuitive.


Equivalent and even simpler:

void foo()
{
IntStack s(20);
s.push_back(20);
}

Remarkably, your code does the first 2 things listed on the
'pet peeves' thread on c.l.c++.moderated, ie. using 'new'
and 'delete' instead of creating an automatic object,
and catching something only to rethrow it.

Aug 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
7223
by: Stephan | last post by:
why does the following code not work???? after compiling and running it will just say killed after all my memory filled up any suggestions? #include <iostream> using namespace std; void...
5
2578
by: Bikash | last post by:
Hello, I am a specific problem in exception handling. The code snippets is attached below. void f() { char *ptr = new char(20); throw 2; }
10
2253
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
16
2488
by: Jacob | last post by:
It is common practice (I've heard) to always catch allocation exceptions from "new". How is done in practice? What would be a common procedure (path of sequence) after such an event has occured?...
6
6533
by: mangesh | last post by:
1 - How to cach invalid memory access exception ? Does standard library provide any help ? 2 - Also when one write catch(...) { //........... } what is wirtten inside catch block . How do...
20
9329
by: mariano.difelice | last post by:
Hi, I've a big memory problem with my application. First, an example: If I write: a = range(500*1024) I see that python process allocate approximately 80Mb of memory.
2
4030
by: jayapal | last post by:
Hi , I am using the NEW operator to allocate the memory in many places of my code.But I am not doing any error hadling or exception handling.Can any one suggests me how to do exception handling,...
1
3090
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
2
2441
by: Lambda | last post by:
I'd like to load a lot of data into a hashtable, when the memory is used up, I'll write the data to a file. I'm trying to use std::tr1::unordered_map to implement that. My question is if I...
0
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5689
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.