Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Exception Handling & Memory Leak

Reply
Thread Tools

Exception Handling & Memory Leak

 
 
Bikash
Guest
Posts: n/a
 
      07-22-2004
Hello,

I am a specific problem in exception handling. The code snippets is
attached below.

void f()
{
char *ptr = new char(20);
throw 2;
}

void main(void)
{
try
{
f();
}
catch(...)
{
}
}

The above function calls shows that a memory has been allocated to
char * pointer. With the throw statment in the subsequent line states
that there will be memory leak in this time of situation. I just
wanted to know is there any method to free the memory allocated in the
catch(...) block.

Regards
Bikash
 
Reply With Quote
 
 
 
 
rokia
Guest
Posts: n/a
 
      07-22-2004
finnaly()
{
}

or use deconstructor .


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-22-2004

"Bikash" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hello,
>
> I am a specific problem in exception handling. The code snippets is
> attached below.
>
> void f()
> {
> char *ptr = new char(20);
> throw 2;
> }
>
> void main(void)
> {
> try
> {
> f();
> }
> catch(...)
> {
> }
> }
>
> The above function calls shows that a memory has been allocated to
> char * pointer. With the throw statment in the subsequent line states
> that there will be memory leak in this time of situation. I just
> wanted to know is there any method to free the memory allocated in the
> catch(...) block.
>


No there isn't.

Don't use raw pointers, put your pointers in classes instead. Classes can
have destructors and so can free the memory of any pointers they hold.

john


 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      07-22-2004
Bikash wrote:

> Hello,
>
> I am a specific problem in exception handling. The code snippets is
> attached below.
>
> void f()
> {
> char *ptr = new char(20);
> throw 2;
> }
>
> void main(void)
> {
> try
> {
> f();
> }
> catch(...)
> {
> }
> }
>
> The above function calls shows that a memory has been allocated to
> char * pointer. With the throw statment in the subsequent line states
> that there will be memory leak in this time of situation. I just
> wanted to know is there any method to free the memory allocated in the
> catch(...) block.


If it is acceptable that the memory is freed when the f() is left via an
exception (i.e. before it enters the catch(...) block) you could use
std::auto_ptr class or a more sophisticated smart pointer like the ones
in the boost library (http://boost.org/). You may also want look at the
RAII idiom, this idiom is essential if you want to write exception safe
code.


--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      07-22-2004
rokia wrote:

> finnaly()
> {
> }



Even though many compilers support it 'finally' is not standard C++.
However the RAII idiom is an excellent alternative for finally.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      07-22-2004
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Bikash) wrote:

>Hello,
>
>I am a specific problem in exception handling. The code snippets is
>attached below.
>
>void f()
>{
> char *ptr = new char(20);
> throw 2;
>}
>
>void main(void)
>{
> try
> {
> f();
> }
> catch(...)
> {
> }
>}
>
>The above function calls shows that a memory has been allocated to
>char * pointer. With the throw statment in the subsequent line states
>that there will be memory leak in this time of situation. I just
>wanted to know is there any method to free the memory allocated in the
>catch(...) block.


There would be a memory leak in any case because no part of the code
even makes the attempt to delete the memory allocated... Maybe a more
resonable example?

void function_that_may_throw();

int main() {
try {
char* ptr = new char( 20 );
function_that_may_throw();
delete ptr;
}
catch ( ... ) { }
}

In this, the solution is to use an auto_ptr:

int main() {
try {
auto_ptr<char> ptr( new char( 20 ) );
function_that_may_throw();
}
catch ( ... ) { }
}

Other examples may call for other solutions, but in all casses RAII is
the way to go. Do a google search on "RAII".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
memory leak with a thrown exception rupert C++ 10 11-08-2006 08:34 AM
Leak or no leak ?? Richard Heathfield C Programming 4 07-10-2006 11:37 AM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM
SoapExtension for Global Exception handling; but prevent exception from propagating!! VSK ASP .Net Web Services 0 07-29-2003 05:39 PM



Advertisments