Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Destructors

Destructors

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicshelpquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lor75
    wrote on last edited by
    #1

    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.

    D S 2 Replies Last reply
    0
    • L lor75

      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.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • D David Crow

        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

        L Offline
        L Offline
        lor75
        wrote on last edited by
        #3

        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?

        M A 2 Replies Last reply
        0
        • L lor75

          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?

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • L lor75

            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?

            A Offline
            A Offline
            Albert Holguin
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • L lor75

              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.

              S Offline
              S Offline
              SILK Packers and movers
              wrote on last edited by
              #6

              SILK Packers and movers in Lahore Karachi islamabad rawalpindi Pakistan http://www.asklint.com/

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups