Memory leakage occurs when exit (0) is used to exit directly.

Source: Internet
Author: User

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 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.