memory leak in constructor and during object creation

Discussion in 'C++' started by raj s, Sep 29, 2008.

  1. raj s

    raj s Guest

    Will the below code cause memory leak in c++

    class base{
    int a;
    int *pint;
    someclass objsomeclass;
    someclass* psomeclass;
    base(){
    objsomeclass = someclass();
    psomeclass = new someclass();
    pint = new int();
    throw "constructor failed";
    a = 43;
    }
    }
    main(){
    base temp();
    }

    in the above code constructor fails.Which objects will be leaked and
    how to avoid memory leak and even if destructor is called for
    automatic object is temp and temp pointer will leak the memory?

    main(){
    base *temp = base();
    }

    what about in the above code and how to avoid memory leak when
    constructor fails
    raj s, Sep 29, 2008
    #1
    1. Advertising

  2. raj s

    Ian Collins Guest

    raj s wrote:
    > Will the below code cause memory leak in c++
    >
    > class base{
    > int a;
    > int *pint;
    > someclass objsomeclass;
    > someclass* psomeclass;
    > base(){
    > objsomeclass = someclass();
    > psomeclass = new someclass();
    > pint = new int();
    > throw "constructor failed";
    > a = 43;
    > }
    > }
    > main(){
    > base temp();
    > }
    >
    > in the above code constructor fails.Which objects will be leaked and
    > how to avoid memory leak and even if destructor is called for
    > automatic object is temp and temp pointer will leak the memory?
    >

    Wrap them in something that frees the memory, such as auto_ptr or an
    appropriate smart pointer.

    > main(){
    > base *temp = base();
    > }
    >
    > what about in the above code and how to avoid memory leak when
    > constructor fails


    There isn't one.

    --
    Ian Collins.
    Ian Collins, Sep 29, 2008
    #2
    1. Advertising

  3. raj s

    Old Wolf Guest

    On Sep 29, 7:33 pm, raj s <> wrote:
    > Will the below code cause memory leak in c++
    >
    > class base{
    >     int a;
    >     int *pint;
    >     someclass objsomeclass;
    >     someclass* psomeclass;
    >     base(){
    >         objsomeclass = someclass();
    >         psomeclass = new someclass();
    >         pint = new int();
    >         throw "constructor failed";
    >         a = 43;
    >     }}


    Should be a semicolon after this. Now, as
    you suspect, if you create a 'base' then
    you get a memory leak because the memory
    allocated by "new someclass()" and "new int()"
    is never freed.

    The best solution is to design your class
    to not 'new' things. I guess you are coming
    from Java or something like that by your code
    stype. In C++ you rarely need to have classes
    that 'new' their members. For example, in
    the above code, there is no need to 'new' an int.

    Failing that, you will have to use
    'smart pointers' instead of raw pointers.
    For example, if instead of:
    someclass *psomeclass;
    you could have:
    auto_ptr<someclass> psomeclass;

    Note that you can initialize things in
    the constructor initializer list:
    base(): psomeclass( new someclass ) {.....}

    Some comments on the rest of your code:
    >
    > main(){
    >     base temp();


    This doesn't declare any objects, it declares
    a function. I think you meant:
    base temp;

    You seem to have a fascination with useless
    parentheses :)
    Old Wolf, Sep 29, 2008
    #3
  4. raj s

    Fred Zwarts Guest

    "raj s" <> wrote in message news:...
    > Will the below code cause memory leak in c++
    >
    > class base{
    > int a;
    > int *pint;
    > someclass objsomeclass;
    > someclass* psomeclass;
    > base(){
    > objsomeclass = someclass();
    > psomeclass = new someclass();
    > pint = new int();
    > throw "constructor failed";
    > a = 43;
    > }
    > }
    > main(){
    > base temp();
    > }
    >
    > in the above code constructor fails.Which objects will be leaked and
    > how to avoid memory leak and even if destructor is called for
    > automatic object is temp and temp pointer will leak the memory?
    >
    > main(){
    > base *temp = base();
    > }
    >
    > what about in the above code and how to avoid memory leak when
    > constructor fails


    If you really must use new and if you cannot use a smart pointer type,
    than you should handle the exception yourself. E.g.:

    base()
    : pint (0), psomeclass (0)
    {
    try {
    objsomeclass = someclass();
    psomeclass = new someclass();
    pint = new int();
    throw "constructor failed";
    a = 43;
    } catch (...) {
    delete pint;
    delete psomeclass;
    throw;
    }
    }
    Fred Zwarts, Sep 29, 2008
    #4
  5. raj s schrieb:
    > Will the below code cause memory leak in c++


    Yes. All pointer objects will never be destroyed, since your programm
    does not provide a delete statement.

    > class base{
    > int a;
    > int *pint;
    > someclass objsomeclass;
    > someclass* psomeclass;
    > base(){
    > objsomeclass = someclass();
    > psomeclass = new someclass();
    > pint = new int();
    > throw "constructor failed";
    > a = 43;
    > }
    > }
    > main(){
    > base temp();
    > }


    > in the above code constructor fails.Which objects will be leaked and
    > how to avoid memory leak


    Use smart pointers.

    > and even if destructor is called for
    > automatic object is temp and temp pointer will leak the memory?


    The destructor will not be called if the constructor fails. But the
    destructor for a successfully constructed base members is called -
    normally. In your case, you do not catch your exception. That usually
    causes a call to abort(), which will end your programm immediately
    without any cleanup. From that point it is up to the operating system to
    do the cleanup.

    > main(){
    > base *temp = base();
    > }
    >
    > what about in the above code and how to avoid memory leak when
    > constructor fails


    The memory of base is normally freed in this case, since no object is
    successfully constructed. But again, you must catch your exception to
    prevent your programm from an immediate abort.


    Marcel
    Marcel Müller, Sep 29, 2008
    #5
    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. Ben Held
    Replies:
    1
    Views:
    339
    Jon-Pierre Gentil
    Apr 3, 2004
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    677
    Eric Sosman
    Mar 22, 2005
  3. jon wayne
    Replies:
    9
    Views:
    695
    Jim Langston
    Sep 22, 2005
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,197
  5. cham
    Replies:
    5
    Views:
    756
Loading...

Share This Page