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.

Saturday, 27 November 2010

1.2 SCADA - Application

SCADA software/system can be used in different geographical area, from national wide area to small standalone device monitoring.
From software view, it can be separated to 3 types applications: standalone distributed and networked.

Standalone means all software modules located in one computer even just in one program. This model is suitable for small SCADA project in small area.



Standalone System
Distributed system can be used in middle site or large project. In this model, application module is separated in different computer. But they are also installed in same LAN (Local Area Network), because SCADA system need real time response but today’s WAN (Wide Area Network) can not meet this real time requirement or is too expensive.

Distributed System
Networked system is the future direction for SCADA system. In this system, SCADA module can be separated in any place in World Wide Web. This system needs more quick and reliable network. And it also needs the international standard for SCADA system. But in this time, I think it is still a dream.

Networked System

Friday, 12 November 2010

1.1 SCADA - Definition

SCADA stands for Supervisory Control And Data Acquisition. Normally, it is used in several fields:
Industrial Monitoring and Control
Manufacturing Process

图1.1 SCADA framework
Main components are:
SCADA Host: This is the centre computer, it collect, process and stores all data. It can be just a program, or can be a program team.
HMI: This is the client end interface. It stands for Human Machine Interface.
Communication Network: This may be Ethernet, serial port or even wireless network
PLC / RTU: This is the communication equipment. RTU stands for Remote Telecommunication Unit. PLC stands for Programmable Logical Controller. Traditionally, PLC fulfill more control function, RTU can undertake more communication work and can work under more serious environment. But today this difference is dimming.
Device: This is the practical equipment which completes the industrial function, such as pump, valve, or switch.
There are data flows in two ways: Control and Data Acquisition.

0 SCADA - Preface

I want to summarize my past experience and technology, such as C++, SCADA, SDLC. So SCADA will be the first serial. This is first article from a serial. This serial will contain next contents:
SCADA
Definition
Application
Configuration SCADA Software
Framework
Abstraction
Big Names
My Projects & Tools
Electricity
Gas / Oil
So, let us begin.

Tuesday, 9 November 2010

Why 7117

In July, 2001, we spent 1 month to climb the Nyenchen Tanglha. Finally we reached one top of 4 peaks. Its height is 7117. I also recognized something about myself and some real meaning of life.
7117 is a important symbol for me and for my family.
Now we come to a new country and face to a totally different and difficult environment. I hope we can reach the new top also.
Why we want to climb the mountain? Nothing, just cause it is there.
Why we want to go around the world? Nothing, just cause it is there.