Menu

#1 leaks pointer if "new Counter" throws

open
nobody
None
5
2008-03-26
2008-03-26
Anonymous
No

Leaks the pointer that was tried to be stored in yasper::ptr if allocation of a Counter fails in yasper::ptr constructor. I think operator= is affected as well.

Please refer to the attached source code. Expected output would be:
A::A()
A::~A()

but currently is:
A::A()

Discussion

  • Nobody/Anonymous

     
  • Nobody/Anonymous

    Logged In: NO

    sorry I screwed the file before pressing submit and can't post another file
    here's a patch to correct it

    --- leak_fixed.cpp 2008-03-26 10:06:46.000000000 +0200
    +++ leak.cpp 2008-03-26 10:06:52.000000000 +0200
    @@ -1,5 +1,8 @@

    -#include "yasper.h"
    +//#include "yasper.h"
    +#include <tr1/memory>
    +#define yasper::ptr std::tr1::shared_ptr
    +
    #include <cstdlib>
    #include <iostream>
    #include <new>
    @@ -39,7 +42,7 @@
    can_alloc(true);
    A *ptr = new A; // success on "new A"
    can_alloc(false);
    - yasper::ptr<A> sp(ptr); // failure on "new Counter"
    + std::tr1::shared_ptr<A> sp(ptr); // failure on "new Counter"
    }
    catch(...) {}

     

Log in to post a comment.