When exit (0) is used to exit the program, the following Memory leakage information is displayed:
Detected memory leaks!
Dumping objects->
F: \ Sp \ vctools \ vc7libs \ ship \ atlmfc \ SRC \ MFC \ strcore. cpp (141): {178} normal block at 0x003da9b0, 36 bytes long.
Data: <9px> AC 39 50 78 09 00 00 00 09 00 00 01 00 00 00 00
F: \ Sp \ vctools \ vc7libs \ ship \ atlmfc \ SRC \ MFC \ strcore. cpp (141): {177} normal block at 0x003da900, 110 bytes long.
Data: <9px ..> AC 39 50 78 2E 00 00 00 2E 00 00 00 01 00 00
Object dump complete.
Both of them are string problems, which are caused by cstring.
But why does cstring cause it?
The source program is as follows:
{Cstring setpath = l "f :\\"; g_strip = l "127.0.0.1"; clogindlg DLG; DLG. m_strip = g_strip; // m_strip is the cstring variable of DLG g_strip is the global cstring variable if (DLG. domodal () = idok) {g_strip = DLG. m_strip;} else {exit (0 );}}
Analysis:
This program has two local variables.
CString setPath
CLoginDlg dlg;
And a global variable cstring g_strip
The local variable DLG contains the string variable DLG. m_strip.
By default, csting automatically revokes the cstring object after it is out of scope.
However, because exit (0) is used to forcibly exit this scope, the cstring object used in this scope is not released, so Memory leakage occurs.
The global cstring object is not affected. When exit (0) exits the function, the global cstring is automatically released.
Therefore, the culprit of Memory leakage is the cstring local variable in the scope. Since it cannot be automatically revoked, We have to undo it manually. Before forcible exit, add a cstring undo statement to eliminate Memory leakage.
The modified program is as follows:
{Cstring setpath = l "f :\\"; g_strip = l "127.0.0.1"; clogindlg DLG; DLG. m_strip = g_strip; // m_strip is the cstring variable of DLG g_strip is the global cstring variable if (DLG. domodal () = idok) {g_strip = DLG. m_strip;} else {DLG. m_strip.empty (); // undo the setpath of the local cstring object. empty (); exit (0 );}}