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. Closing dialog correctly

Closing dialog correctly

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionperformance
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.
  • J Offline
    J Offline
    jimNLX
    wrote on last edited by
    #1

    I have a dialog app that calls different dialog windows from a menu. Here is the problem: After opening a window from my menu , closing it, and then closing the application I get a memory leak detected error. This is how I establish the dialog: else if(PU_SELECT_ID == ID_PU_MALF_SI) { CMALFSIDlg *pdlg; error pts here->>>pdlg = new CMALFSIDlg; pdlg->Create(IDD_MALF_SI); pdlg->ShowWindow(SW_SHOW); } ???? So if I add a button to close this window how do I do so without having memory leaks? I've tried EndDialog() and DestroyWindow() but they don't seem to take care of the memory leak. Thanks for any help at alL!!!!

    D N T 3 Replies Last reply
    0
    • J jimNLX

      I have a dialog app that calls different dialog windows from a menu. Here is the problem: After opening a window from my menu , closing it, and then closing the application I get a memory leak detected error. This is how I establish the dialog: else if(PU_SELECT_ID == ID_PU_MALF_SI) { CMALFSIDlg *pdlg; error pts here->>>pdlg = new CMALFSIDlg; pdlg->Create(IDD_MALF_SI); pdlg->ShowWindow(SW_SHOW); } ???? So if I add a button to close this window how do I do so without having memory leaks? I've tried EndDialog() and DestroyWindow() but they don't seem to take care of the memory leak. Thanks for any help at alL!!!!

      D Offline
      D Offline
      dazinith
      wrote on last edited by
      #2

      the 'new' keyword allocates a chunk of memory and puts a pointer to it.. your program wont clear out this memory like regular variables when you go out of scope.. after your done with the window just call pdlg->DestroyWindow() and then delete pdlg.. what you should prolly do instead is not use the 'new' keyword, this should do the same:

      CMALFSIDlg dlg;
      pdlg.Create(IDD_MALF_SI);
      pdlg.ShowWindow(SW_SHOW);

      that way you dont alocate the memory yourself, and the memory is handled by windows, and it will free up the memory on its own.. yeah im a newbie, but im tryin! -dz

      B 1 Reply Last reply
      0
      • D dazinith

        the 'new' keyword allocates a chunk of memory and puts a pointer to it.. your program wont clear out this memory like regular variables when you go out of scope.. after your done with the window just call pdlg->DestroyWindow() and then delete pdlg.. what you should prolly do instead is not use the 'new' keyword, this should do the same:

        CMALFSIDlg dlg;
        pdlg.Create(IDD_MALF_SI);
        pdlg.ShowWindow(SW_SHOW);

        that way you dont alocate the memory yourself, and the memory is handled by windows, and it will free up the memory on its own.. yeah im a newbie, but im tryin! -dz

        B Offline
        B Offline
        Bubba2146
        wrote on last edited by
        #3

        I don't this approach will do any good. The reason is the dialog is created on the stack, which you stated correctly. However, since it's created on the stack it will also be destroyed when the function exits. So you'll never see the dialog. Do a search for a lesson on multiple dialogs, but it amounts to catching the "close" messages and calling 'delete' on the right pointer. This will free the memory allocated (like you said).

        1 Reply Last reply
        0
        • J jimNLX

          I have a dialog app that calls different dialog windows from a menu. Here is the problem: After opening a window from my menu , closing it, and then closing the application I get a memory leak detected error. This is how I establish the dialog: else if(PU_SELECT_ID == ID_PU_MALF_SI) { CMALFSIDlg *pdlg; error pts here->>>pdlg = new CMALFSIDlg; pdlg->Create(IDD_MALF_SI); pdlg->ShowWindow(SW_SHOW); } ???? So if I add a button to close this window how do I do so without having memory leaks? I've tried EndDialog() and DestroyWindow() but they don't seem to take care of the memory leak. Thanks for any help at alL!!!!

          N Offline
          N Offline
          Navin
          wrote on last edited by
          #4

          iF you are using MFC, before the program exits, you'll go into the app's ExitInstance function. Assuming you store a pointer to this dialog in the app class (or can get at it from there), you should call this in the ExitInstance function: delete pdlg; (Note: to be robust, you should ensure that either pdlg points to a valid dialog, or to NULL!) Even if you win the rat race, you're still a rat.

          1 Reply Last reply
          0
          • J jimNLX

            I have a dialog app that calls different dialog windows from a menu. Here is the problem: After opening a window from my menu , closing it, and then closing the application I get a memory leak detected error. This is how I establish the dialog: else if(PU_SELECT_ID == ID_PU_MALF_SI) { CMALFSIDlg *pdlg; error pts here->>>pdlg = new CMALFSIDlg; pdlg->Create(IDD_MALF_SI); pdlg->ShowWindow(SW_SHOW); } ???? So if I add a button to close this window how do I do so without having memory leaks? I've tried EndDialog() and DestroyWindow() but they don't seem to take care of the memory leak. Thanks for any help at alL!!!!

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            In the dialog, add a handler for OnNCDestroy. Inside that, do

            delete this;

            This might work. It has been a while. Tim Smith "Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution." Hoare - 1980 ACM Turing Award Lecture

            J 1 Reply Last reply
            0
            • T Tim Smith

              In the dialog, add a handler for OnNCDestroy. Inside that, do

              delete this;

              This might work. It has been a while. Tim Smith "Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution." Hoare - 1980 ACM Turing Award Lecture

              J Offline
              J Offline
              jimNLX
              wrote on last edited by
              #6

              I've tried doing the deletes and destroys, but it just doesn't work. I have the pointers defind under the OnMouseBtnClick and when I go to delete VC says that it is undefined. I'm trying to find the cleanest and most modular way to do this, am I going to have to define these pointer ahead of the menu call making them global to the rest of the code?

              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