Memory leakage that is hard to detect

Source: Internet
Author: User

Memory leakage that is hard to detect

University of Electronic Science and Technology
Zhu Ning

---- Recently, when I used VC ++ to write a screen saver, I was very confused about memory leakage. Later, the problem was solved based on several examples. Let's analyze the cause and solution:

---- The author derives a subclass cdrawwnd from cwnd and adds the following code to the initinstance () of the main program class:

m_pMainWnd=new CDrawWnd();m_pMainWnd- >Create(.....);return TRUE;

---- After debugging and running, the VC ++ debugger reports Memory leakage. Obviously, the problem only occurs in the first line of code above. When I wrote the above code, I also suspected that there is no corresponding delete for new, which will lead to heap memory unrecoverable. However, refer to the example of VC ++ 5.0 "hello ", the code of this program is similar to the above, and no other function calls Delete to reclaim heap memory, but it does not cause memory leakage. Check the online help of VC ++ without detailed instructions. It only means that when the window is closed, the virtual function postncdestory is automatically called to reclaim the memory directed by m_pmainwnd.

---- So I tried to call Delete m_pmainwnd in the main program class's destructor, which has no effect. Explicit call of cwnd: destorywindow () does not work either. By referring to the example saver, we found that the implementation code in front of it is exactly the same as the author's. However, it reloads the postncdestory virtual function in the cdrawwnd class, with a line of key code:

delete this;

---- In fact, it destroys the cdrawwnd object.

---- So I immediately modified my program, reloaded the postncdestory function in the main window class, and added the above sentence to immediately solve the memory leakage problem.

---- The reason is finally clear. Originally, if the main window class of the program is inherited from cframewnd, the memory occupied by the class object does not need to be manually recycled. In the example of "hello", this is a program. However, if the main window class is directly inherited from the cwnd class, you must consider this issue.

 

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.