Destructors
-
Hello, I’m new in MFC programming and I have a doubt about destructor's calling in a MFC application. I wonder if it’s necessary to insert destructors for the classes of the project. I suppose (but I’m not sure) that Windows automatically calls destructors for classes created by the project wizard (doc, view, frame…) when I stop the program execution and close the program window. But what’s the situation for classes that I have inserted in the software? I never put destructor for these objects (dialogs, strings, bitmap, controls, custom objects…) Is this a problem? What could be the problems in this situation? Best regards.
-
Hello, I’m new in MFC programming and I have a doubt about destructor's calling in a MFC application. I wonder if it’s necessary to insert destructors for the classes of the project. I suppose (but I’m not sure) that Windows automatically calls destructors for classes created by the project wizard (doc, view, frame…) when I stop the program execution and close the program window. But what’s the situation for classes that I have inserted in the software? I never put destructor for these objects (dialogs, strings, bitmap, controls, custom objects…) Is this a problem? What could be the problems in this situation? Best regards.
lor75 wrote:
I wonder if it’s necessary to insert destructors for the classes of the project.
Yes, if you have clean up tasks that need to be done.
lor75 wrote:
I suppose (but I’m not sure) that Windows automatically calls destructors for classes...
Yes it does.
lor75 wrote:
But what’s the situation for classes that I have inserted in the software?
Please explain.
lor75 wrote:
I never put destructor for these objects (dialogs, strings, bitmap, controls, custom objects…)
You mean classes that you're using as opposed to those that you've created?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
lor75 wrote:
I wonder if it’s necessary to insert destructors for the classes of the project.
Yes, if you have clean up tasks that need to be done.
lor75 wrote:
I suppose (but I’m not sure) that Windows automatically calls destructors for classes...
Yes it does.
lor75 wrote:
But what’s the situation for classes that I have inserted in the software?
Please explain.
lor75 wrote:
I never put destructor for these objects (dialogs, strings, bitmap, controls, custom objects…)
You mean classes that you're using as opposed to those that you've created?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
I try to explain with an example: I create a CBitmap object used to put a bitmap on a custom button control. Do I have to destroy the object when I exit from the program or the object is automatically destroied?
Is the CBitmap object allocated on the heap (with new) or on the stack
class YourClass
{
~YourClass (){/* no need to delete m_Bitmap */};
CBitmap m_Bitmap;
};or
class YourClass
{
~YourClass () {delete m_Bitmap;};
CBitmap* m_Bitmap;
};If you properly override a MFC class then the MFC (and/or underlying Win32 object) will be destructed by the framework, but everything else that you have allocated yourself should be handled by you (in a cleanup function or in the class destructor).
I'd rather be phishing!
-
I try to explain with an example: I create a CBitmap object used to put a bitmap on a custom button control. Do I have to destroy the object when I exit from the program or the object is automatically destroied?
If the CBitmap was dynamically allocated, then you probably need to deallocate it yourself. If it was allocated on the stack, then the framework will take care of it. For the most part, anything you new, you must delete. As for classes that you make, you only really need an explicit destructor if you need to clean things up that you did (deallocate memory/resources, close files, close sockets, etc). The framework has default destructors that deallocate what it allocated behind the scenes, so you just have to clean up what you did for the most part.
-
Hello, I’m new in MFC programming and I have a doubt about destructor's calling in a MFC application. I wonder if it’s necessary to insert destructors for the classes of the project. I suppose (but I’m not sure) that Windows automatically calls destructors for classes created by the project wizard (doc, view, frame…) when I stop the program execution and close the program window. But what’s the situation for classes that I have inserted in the software? I never put destructor for these objects (dialogs, strings, bitmap, controls, custom objects…) Is this a problem? What could be the problems in this situation? Best regards.
SILK Packers and movers in Lahore Karachi islamabad rawalpindi Pakistan http://www.asklint.com/