Saturday, 14 May 2011

Virtual Destructor and Memory Leak

I met a memory leak problem when I debug a small application. When application exist, the memory leak detector always report that there are some memory leaks. There just some information about block number. It looks like following:

Detected memory leaks!
Dumping objects ->
{60} normal block at 0x00324818, 4 bytes long.
Data: <, > 2C 00 00 00
Object dump complete.

There is not cpp file name and line number. So I can’t find the place directly. I add a memory allocation detection function to locate this memory leak. This method is just valid when the memory leak always appears in same place no matter application runs how much times.

_CrtSetBreakAlloc(60)

After that, I located memory leak here:

CString szRet;
   GetPrivateProfileString (TEXT("Section1"),
                            TEXT("FirstKey"),
                            TEXT("Error: GPPS failed"),
                            szRet,
                            80,
                            TEXT("appname.ini"));
m_szKey = szRet;

It means the m_szKey is allocated but never released. I checked the class definition file. m_szKey is declared as a normal variable.

CString m_szKey;

It is strange. Because m_szKey is just a normal variable, it will be destroyed automatically when object is released, event the destructor didn't destroy it explicitly. Could it been said that this object’s destructor is not been called? Then I checked definition of this class. It looks like following:

Class A
{
       public:
              A();
              ~A();
}

Class B : public A
{
       public:
              B();
~B();
       private:
              CString m_szKey;
}

In application, B was used like this:

CList<A*, A*> lstAB;
B* b = new B();
lstAB.add(b);

Right now, you should find out the reason for memory leak. Parent class A didn’t declare its destructor as virtual. In application, B’s object was added in a list which is declared as A*. This is a normal usage of Polymorphism. When we called some object from this list, this object may behave like A or like B. It depends on its real type. But when this list was destroyed, all objects inside this list just treated as A. Just ~A() was called and ~B() never been called. m_szKey is B’s private member and never been destroyed.

I changed this code and declared ~A() as virtual. I run application again. Ok, memory leak disappeared.

Thursday, 24 February 2011

Who can save you?


I studied in a Bible reading group. One day we discussed a topic about how to follow God.
God said we can discard everything except God because he will stay with us forever and look after us. Our study mates argue some tiny different meaning in these paragraphs. But I just think another question. What we should do besides God’s care? I heard a joke.

There was a flood. A man climbed up to roof and waited for a rescue. He didn’t worry about his situation because he believes god. He thought: I don’t worry, God will save me.

Then there was a helicopter. But the man said: I didn’t take it. I believe God. God will save me. Helicopter left and the water was rising.

Then there was a ship. The man said: I didn’t take it. I believe God. God will save me. Ship left and the water was rising.

Then there war a big floating wood. But the man said: I didn’t take it. I believe God. God will save me. The wood left and the water was rising.

Finally, this man died. He was very angry when he saw God. He asked: God, I believe you. Why you didn’t save my life. God was cross: Dear, I sent out a helicopter, a ship and a floating wood. Why you didn’t take one?

So I think nobody can save your life even God, if you didn’t want save yourself.

Little Boy and Big Thinker

My son is just 3 years old. But he has many questions now. Some questions are very interesting and are hard to answer.
One day, he asked: Dad, when I grow up, how about you?
I said: I will be old.
Then, he asked: How about you when I am old?
I thought for 1 second, and answered hardly: I will die.
He didn't say anything for this and kept playing with his toys. After 1 minute, he said suddenly: It will be great if we live forever and never die.
I didn't cry at that moment because I am an adult.