Memory leak ?

Discussion in 'C++' started by Felix Sima, Feb 17, 2004.

  1. Felix Sima

    Felix Sima Guest

    Hi.

    Does the following line generate a memory leak or doesn't ?

    *** foo( new CTest( ) ); *** //See code below.

    Watching the output of the program seems that only the constructor
    of the temporary object is called so that the memory allocated for
    "new CTest( )" doesn't get deallocated.

    Is it the right behaviour? If not, what can I do ?

    Thank you.
    Felix

    ********* CODE *********
    #include <iostream>
    using namespace std;

    class CTest
    {
    public:
    CTest()
    {
    cout << "Constructor" << endl;
    };
    ~CTest()
    {
    cout << "Destructor" << endl;
    };
    };

    void foo( CTest *p )
    {
    };

    int main( int argc, char **argv )
    {
    foo( new CTest( ) ); //Memory leak here ?

    return 0;
    };

    ******** OUTPUT ************
    Constructor
     
    Felix Sima, Feb 17, 2004
    #1
    1. Advertising

  2. "Felix Sima" <> skrev i en meddelelse
    news:c0t9qb$4gk$...
    > Hi.
    >
    > Does the following line generate a memory leak or doesn't ?
    >
    > *** foo( new CTest( ) ); *** //See code below.
    >
    > Watching the output of the program seems that only the constructor
    > of the temporary object is called so that the memory allocated for
    > "new CTest( )" doesn't get deallocated.
    >
    > Is it the right behaviour? If not, what can I do ?
    >
    > Thank you.
    > Felix
    >
    > ********* CODE *********
    > #include <iostream>
    > using namespace std;
    >
    > class CTest
    > {
    > public:
    > CTest()
    > {
    > cout << "Constructor" << endl;
    > };
    > ~CTest()
    > {
    > cout << "Destructor" << endl;
    > };
    > };
    >
    > void foo( CTest *p )
    > {
    > };
    >
    > int main( int argc, char **argv )
    > {
    > foo( new CTest( ) ); //Memory leak here ?
    >
    > return 0;
    > };
    >
    > ******** OUTPUT ************
    > Constructor
    >


    Of course. For every new there should be a corresponding delete. Do you come
    from Java? ;-)

    /Peter
     
    Peter Koch Larsen, Feb 17, 2004
    #2
    1. Advertising

  3. LOL what about C# or Visual Basic..
    hahaha

    "Peter Koch Larsen" <> wrote in message
    news:BtqYb.91829$...
    >
    > "Felix Sima" <> skrev i en meddelelse
    > news:c0t9qb$4gk$...
    > > Hi.
    > >
    > > Does the following line generate a memory leak or doesn't ?
    > >
    > > *** foo( new CTest( ) ); *** //See code below.
    > >
    > > Watching the output of the program seems that only the constructor
    > > of the temporary object is called so that the memory allocated for
    > > "new CTest( )" doesn't get deallocated.
    > >
    > > Is it the right behaviour? If not, what can I do ?
    > >
    > > Thank you.
    > > Felix
    > >
    > > ********* CODE *********
    > > #include <iostream>
    > > using namespace std;
    > >
    > > class CTest
    > > {
    > > public:
    > > CTest()
    > > {
    > > cout << "Constructor" << endl;
    > > };
    > > ~CTest()
    > > {
    > > cout << "Destructor" << endl;
    > > };
    > > };
    > >
    > > void foo( CTest *p )
    > > {
    > > };
    > >
    > > int main( int argc, char **argv )
    > > {
    > > foo( new CTest( ) ); //Memory leak here ?
    > >
    > > return 0;
    > > };
    > >
    > > ******** OUTPUT ************
    > > Constructor
    > >

    >
    > Of course. For every new there should be a corresponding delete. Do you

    come
    > from Java? ;-)
    >
    > /Peter
    >
    >
     
    Martin SChukrazy, Feb 17, 2004
    #3
  4. Felix Sima <> wrote in message news:<c0t9qb$4gk$>...
    > Does the following line generate a memory leak or doesn't ?
    >
    > *** foo( new CTest( ) ); *** //See code below.


    It's not that specific line that generates a memory leak, but the
    design of all your code. The problem isn't that you new it, but that
    you have no matching delete.

    samuel
     
    Ron Samuel Klatchko, Feb 18, 2004
    #4
  5. Felix Sima wrote:
    >
    > Hi.
    >
    > Does the following line generate a memory leak or doesn't ?
    >
    > *** foo( new CTest( ) ); *** //See code below.
    >
    > Watching the output of the program seems that only the constructor
    > of the temporary object is called so that the memory allocated for
    > "new CTest( )" doesn't get deallocated.
    >
    > Is it the right behaviour? If not, what can I do ?
    >


    Not doing dynamic allocation. Then you don't have to worry about freeing
    it. ANd of course, not using pointers if there is no need to.

    You could eg. do:

    void foo( const CTest& Arg )
    {
    }

    int main()
    {
    foo( CTest() );
    }

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Feb 18, 2004
    #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. =?Utf-8?B?Y3liZXJzdHJpa2U=?=

    datagrid memory leak?

    =?Utf-8?B?Y3liZXJzdHJpa2U=?=, Jan 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    472
    =?Utf-8?B?Y3liZXJzdHJpa2U=?=
    Jan 3, 2005
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    720
    Eric Sosman
    Mar 22, 2005
  3. Richard Heathfield

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Replies:
    4
    Views:
    364
    Richard Heathfield
    Jul 10, 2006
  4. cham
    Replies:
    5
    Views:
    776
  5. Mark Probert
    Replies:
    4
    Views:
    339
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page