Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I have a CList(BaseList) that takes a base class pointer as a argument as shown in below code.My doubt is whether BaseList will delete the d1 pointer when it goesd out of the scope or i have to delete it manually(delete d1).can you please provide any article on this.
C#
class CBase
{
public:
    CBase()
    {
        cout<<"in CBase Constructor::"<<this<<endl;
    }
    virtual ~CBase()
    {
        cout<<"in CBase Destructor::"<<this<<endl;
    }
    virtual void fun1() = 0;
    virtual void fun2() = 0;
};
class CDerived1:public CBase
{
public:
    CDerived1()
    {
        cout<<"in CDerived Constructor::"<<this<<endl;
    }
    virtual ~CDerived1()
    {
        cout<<"in CDerived Destructor::"<<this<<endl;
    }
    void fun1(){};
    void fun2(){};
};
typedef CList<CBase*> BaseList;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;
    BaseList ouBaseList;
    CBase* d1 = new CDerived1();
    //CDerived1 d1;
    ouBaseList.AddHead(d1);
    //delete d1;
    return nRetCode;
}

I believe it doesn't it doesn't.

You can find out yourself however.
Place the following at the top of the function _tmain
#if defined(DEBUG) | defined(_DEBUG)
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif


In short, this will print memory leaks if there are any to the Output Window (where you see the compiling progress and loaded modules). If nothing is printed, there are no leaks.

I would recommend putting this as early as possible in every program that you write.
It will help you find memory leaks. If you put it in when you start the project it will narrow down the possibilities of where it could be.

If any leaks are found, a number and small portion of the data is printed.
if you use the function _CrtSetBreakAlloc(the_number); it will break the program when that memory block is getting allocated.
 
Share this answer
 
Comments
Thank you for the info.I have seen that memory for d1 is not deleted and output window is showing 4bytes of memory leak.But i did not included the lines,

#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif

But output window is showing them automatically.
Andrew Brock 9-Feb-11 5:56am    
In that case your debug version of the CRT has the flags _CRTDBG_ALLOC_MEM_DF and _CRTDBG_LEAK_CHECK_DF set by default.
Generally speaking, if you new an object you must delete it. "New" creates objects on the heap that must be manually deleted, sometimes the framework deletes some things automatically but generally you do it manually. Whenever writing unmanaged C++, match every new with a delete.
 
Share this answer
 
Comments
Espen Harlinn 17-Mar-11 10:45am    
Good advice - my 5
Most generic collections does not delete pointers. Consider:
CList<int> intList;
intList.AddHead(1); // cannot delete

CList<char*> charPtrList;
charPtrList.AddHead("hello"); // Cannot free
charPtrList.AddHead(new char); // Could free with delete
charPtrList.AddHead(new char[10]); // Could free, but with delete[]
charPtrList.AddHead(strdup("hello")); // Could free, but with free()

It would be a mess to auto-delete data in a generic way.
 
Share this answer
 
Comments
Thank you for the clarification from the first answer i came to know that auto-delete is not possible and from this answere i came to know why it is not possible.
Hi,

No the memory allocated to d1 will not be freed even when the baselist goes out of scope. you have to call the delete for d1 explicitly. If the STL container scope is exited all objects within that container are reclaimed automatically. But only non-pointer types are reclaimed, the data of stored pointers is untouched.
 
Share this answer
 
v2
Comments
Yes you are right.I written a sample code and i came to know that non pointer types are automatically reclaimed.thanks for the answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900