Must new and delete be used in pairs?

Source: Internet
Author: User

For the title of this article, I believe many people will say yes like me two weeks ago.
Please refer to my experiences below --

Two weeks ago, an inexplicable error occurred when the program was closed.

After searching for a long time, it was found that an error occurred while deleting a pointer during exitinstance, which pointed to the cframewnd subclass,
At this time, the pointer value is 0 xdddddddd, which is destined to be an unusual address: deleted.


Why has it been deleted? The answer is in msdn --

Do not use the C ++DeleteOperator to destroy a frame window. UseCwnd: destroywindowInstead.CframewndImplementationPostncdestroyWill delete the C ++ object when the window is destroyed.
When the user closes the frame window, the defaultOncloseHandler will callDestroywindow.

Otherwise, destroywindow is called, and eventuallyPostncdestroyCalled.
The following MFC functions are implemented in the c: \ Program Files \ Microsoft Visual Studio 9.0 \ Vc \ atlmfc \ SRC \ MFC \ winfrm. cpp file.

void CFrameWnd::PostNcDestroy(){// default for frame windows is to allocate them on the heap//  the default post-cleanup is to 'delete this'.// never explicitly call 'delete' on a CFrameWnd, use DestroyWindow insteaddelete this;}

The red part is already quite clear. You don't need to explain it again. So the answer to the title is --
[This sentence may be true in Standard C ++, but it is not necessarily true that MFC "takes the initiative" to do a lot of work for us after it simply looks at our own code .]


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.