Memory leak due to new object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • venuparva
    New Member
    • Jul 2010
    • 6

    Memory leak due to new object

    class1::start()

    {
    test1() ;

    }
    ------------------------------------------------------------------------------------
    class2::test1()

    {

    ret_status = test2();

    }
    -------------------------------------------------------------------
    class3::test2()

    {

    temp_stop = new Groupclass(this );//Groupclass is the name of the some other class

    test3(temp_stop );

    }
    ---------------------------------------------------------------------------
    class4::test3( const char * attr )
    {

    status = test4( attr );
    }
    -------------------------------------------------------------------------------
    class5::test4(c onst char * attr_name )
    {


    set_status = test5( d_string );


    }
    --------------------------------------------------------------------------------------------------
    class6::test5( const char *con_string)
    {


    result =test6( con_string);

    temp1( result);

    result = test7(con_strin g);

    temp2(result);

    }
    --------------------------------------------------------------------------------
    class7::test6( const char *string_to_tran sfer )
    {

    delete[] pstring;

    pstring = test7( string_to_trans fer );

    if( pstring != NULL ) {
    memcpy( (char *) &pstring[sizeof( int )] );
    }
    return pstring;

    }
    --------------------------------------------------------------------------------
    class8::test7( const char *string_to_tran sfer )
    {

    pstring = buffers.test8() ;
    return pstring;

    }
    ---------------------------------------------------------------------------------------
    class9::test8()
    {

    group = new char[totalsize + 1];
    k = group;
    memcpy(k);
    group[totalsize] = '\0';
    return group;

    }

    ----------------------------------------------------------
    I got the memory leak is reported in test8 function ,I am not sure that the mmeory leak in this test8 function only,Could any one go through the code and help me where the memork leak is occuring

    Thanks
    Muthu
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Passing allocated pointer around code is one of the most common ways to produce obscure leaks and crashes.

    When you are inside a function and you have a pointer then:
    1) if you delete and there is another copy of the pointer in the program, you will crash at some point
    2) if you delete and the data was not allocated in the first place, you crash
    3) if you don't delete, then you leak


    I suggest you use a Handle class object instead of a pointer. These objects are also known as smart pointers.

    There is an article on Handle classes in the C/C++ Insights.

    Comment

    Working...