Exception Handling & Memory Leak

Discussion in 'C++' started by Bikash, Jul 22, 2004.

  1. Bikash

    Bikash Guest

    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
     
    Bikash, Jul 22, 2004
    #1
    1. Advertising

  2. Bikash

    rokia Guest

    finnaly()
    {
    }

    or use deconstructor .
     
    rokia, Jul 22, 2004
    #2
    1. Advertising

  3. "Bikash" <> wrote in message
    news:...
    > 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
     
    John Harrison, Jul 22, 2004
    #3
  4. 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
     
    Peter van Merkerk, Jul 22, 2004
    #4
  5. 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
     
    Peter van Merkerk, Jul 22, 2004
    #5
  6. Bikash

    Daniel T. Guest

    In article <>,
    (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".
     
    Daniel T., Jul 22, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    763
    Eric Sosman
    Mar 22, 2005
  2. rupert
    Replies:
    10
    Views:
    477
  3. cham
    Replies:
    5
    Views:
    794
  4. Peter
    Replies:
    34
    Views:
    2,020
    James Kanze
    Oct 17, 2009
  5. Mark Probert
    Replies:
    4
    Views:
    357
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page