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. Help please : "Detected memory leaks!"

Help please : "Detected memory leaks!"

Scheduled Pinned Locked Moved C / C++ / MFC
c++debuggingperformancehelpquestion
10 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.
  • M Offline
    M Offline
    Mandalay
    wrote on last edited by
    #1

    hai ... a have some problems with memory when creating Tabbed Dialogs ... (when TCITEM.lParam = pointer to dialog ..) every things i do correct, but after closing program debugger sad: Detected memory leaks! Dumping objects -> E:\home\projects\kTalk\MainDlg.cpp(343) : {105} client block at 0x00345438, subtype 0, 160 bytes long. a CDialog object at $00345438, 160 bytes long ... and e.t.c ... ==================== ovverriding i also destroying them ... CWnd *pWnd = (LPARAM)tci.lParam; delete pWnd; =============== maybe i should destroy them in other way ?? thanks ---------------- muaaa .. (:

    C J 2 Replies Last reply
    0
    • M Mandalay

      hai ... a have some problems with memory when creating Tabbed Dialogs ... (when TCITEM.lParam = pointer to dialog ..) every things i do correct, but after closing program debugger sad: Detected memory leaks! Dumping objects -> E:\home\projects\kTalk\MainDlg.cpp(343) : {105} client block at 0x00345438, subtype 0, 160 bytes long. a CDialog object at $00345438, 160 bytes long ... and e.t.c ... ==================== ovverriding i also destroying them ... CWnd *pWnd = (LPARAM)tci.lParam; delete pWnd; =============== maybe i should destroy them in other way ?? thanks ---------------- muaaa .. (:

      C Offline
      C Offline
      CoY0te
      wrote on last edited by
      #2

      I could be just guessing (as usual, but finding a bug is just a matter of a right guess anyway). I'm just gonna write some typical bugs (i've had these problems myself recently): 1. Before deleting a window (CWnd object) destroy a window with ->DestroyWindow() unless it isn't done earlier. 2. Before destroying a window make sure it doesn't own some objects like brushes, device contexts etc. - it could be it 3. before destroying any brush/context etc. make sure it's not selected by other objects - that's something I'm aware of very much 4. before creating any object destroy the previous one (example - I've been calling CBrush::CreateSolidBrush twice for the same CBrush object without calling CBrush::DeleteObject) 5. Maybe that tab control is the reason (it or it's elements are not deleted or destroyed - read carefully what they say about deleting it and it's structures) Remember - be patient :-D (yeah, right... :suss: ) Greetings to all assembler dudes and especially for C64 demoscene!

      M 1 Reply Last reply
      0
      • M Mandalay

        hai ... a have some problems with memory when creating Tabbed Dialogs ... (when TCITEM.lParam = pointer to dialog ..) every things i do correct, but after closing program debugger sad: Detected memory leaks! Dumping objects -> E:\home\projects\kTalk\MainDlg.cpp(343) : {105} client block at 0x00345438, subtype 0, 160 bytes long. a CDialog object at $00345438, 160 bytes long ... and e.t.c ... ==================== ovverriding i also destroying them ... CWnd *pWnd = (LPARAM)tci.lParam; delete pWnd; =============== maybe i should destroy them in other way ?? thanks ---------------- muaaa .. (:

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        I'm not sure whether this will work, but it's simple to try. Override PostNcDestroy for your tabbed dialogs and add delete this as the last line of the function. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        C M 2 Replies Last reply
        0
        • J Joaquin M Lopez Munoz

          I'm not sure whether this will work, but it's simple to try. Override PostNcDestroy for your tabbed dialogs and add delete this as the last line of the function. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          C Offline
          C Offline
          CoY0te
          wrote on last edited by
          #4

          ...but keep in mind that "delete this" can be dangerous ;) Just kiddin'. Although delete operator removes the object, the code always remain in the memory (one for all objects of course). I've just never seen anybody using "delete this". Greetings to all assembler dudes and especially for C64 demoscene!

          J 1 Reply Last reply
          0
          • C CoY0te

            ...but keep in mind that "delete this" can be dangerous ;) Just kiddin'. Although delete operator removes the object, the code always remain in the memory (one for all objects of course). I've just never seen anybody using "delete this". Greetings to all assembler dudes and especially for C64 demoscene!

            J Offline
            J Offline
            Joaquin M Lopez Munoz
            wrote on last edited by
            #5

            I agree with you the procedure is a little weird, but this is the standard way to cleanup MFC modeless dialogs, for instance. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            1 Reply Last reply
            0
            • C CoY0te

              I could be just guessing (as usual, but finding a bug is just a matter of a right guess anyway). I'm just gonna write some typical bugs (i've had these problems myself recently): 1. Before deleting a window (CWnd object) destroy a window with ->DestroyWindow() unless it isn't done earlier. 2. Before destroying a window make sure it doesn't own some objects like brushes, device contexts etc. - it could be it 3. before destroying any brush/context etc. make sure it's not selected by other objects - that's something I'm aware of very much 4. before creating any object destroy the previous one (example - I've been calling CBrush::CreateSolidBrush twice for the same CBrush object without calling CBrush::DeleteObject) 5. Maybe that tab control is the reason (it or it's elements are not deleted or destroyed - read carefully what they say about deleting it and it's structures) Remember - be patient :-D (yeah, right... :suss: ) Greetings to all assembler dudes and especially for C64 demoscene!

              M Offline
              M Offline
              Mandalay
              wrote on last edited by
              #6

              But i have done it !!! (: there is my DEL proc : int CMainDlg::DelTab(int Index) { TC_ITEM tci; CChatDlg *pWnd; int iCount = m_tabs.GetItemCount(); int iCurrent = m_tabs.GetCurSel(); m_tabs.GetItem(Index,&tci); ASSERT(tci.lParam); pWnd = (CChatDlg *)tci.lParam; //CChatDlg is CDialog pWnd->DestroyWindow(); pWnd = NULL; delete pWnd; m_tabs.DeleteItem(Index); }; Sometimes its works... but i can't found a bug .. (: there it is .. !? ---------------- muaaa .. (:

              N A 2 Replies Last reply
              0
              • J Joaquin M Lopez Munoz

                I'm not sure whether this will work, but it's simple to try. Override PostNcDestroy for your tabbed dialogs and add delete this as the last line of the function. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                M Offline
                M Offline
                Mandalay
                wrote on last edited by
                #7

                thanks. hehe. it works, no errors, no leaks .. no warnings ... but sometimes ... it works without "delete this". i couldnt understand. (: ---------------- muaaa .. (:

                1 Reply Last reply
                0
                • M Mandalay

                  But i have done it !!! (: there is my DEL proc : int CMainDlg::DelTab(int Index) { TC_ITEM tci; CChatDlg *pWnd; int iCount = m_tabs.GetItemCount(); int iCurrent = m_tabs.GetCurSel(); m_tabs.GetItem(Index,&tci); ASSERT(tci.lParam); pWnd = (CChatDlg *)tci.lParam; //CChatDlg is CDialog pWnd->DestroyWindow(); pWnd = NULL; delete pWnd; m_tabs.DeleteItem(Index); }; Sometimes its works... but i can't found a bug .. (: there it is .. !? ---------------- muaaa .. (:

                  N Offline
                  N Offline
                  Nathan Blomquist
                  wrote on last edited by
                  #8

                  Mandalay wrote: _there is my DEL proc :

                  int CMainDlg::DelTab(int Index)
                  {
                  TC_ITEM tci;
                  CChatDlg *pWnd;
                  int iCount = m_tabs.GetItemCount();
                  int iCurrent = m_tabs.GetCurSel();
                  m_tabs.GetItem(Index,&tci);
                  ASSERT(tci.lParam);
                  pWnd = (CChatDlg *)tci.lParam; //CChatDlg is CDialog
                  pWnd->DestroyWindow();
                  pWnd = NULL; // why do you do this?
                  delete pWnd; // this is a null pointer, why delete it?
                  m_tabs.DeleteItem(Index);
                  };

                  Sometimes its works... but i can't found a bug .. (: there it is .. !?_ Why do you set the pointer to NULL before deletion? Perhaps I missed something in another reply to this message... Later, Nathan --------------------------- Hmmm... what's a signature?

                  C 1 Reply Last reply
                  0
                  • M Mandalay

                    But i have done it !!! (: there is my DEL proc : int CMainDlg::DelTab(int Index) { TC_ITEM tci; CChatDlg *pWnd; int iCount = m_tabs.GetItemCount(); int iCurrent = m_tabs.GetCurSel(); m_tabs.GetItem(Index,&tci); ASSERT(tci.lParam); pWnd = (CChatDlg *)tci.lParam; //CChatDlg is CDialog pWnd->DestroyWindow(); pWnd = NULL; delete pWnd; m_tabs.DeleteItem(Index); }; Sometimes its works... but i can't found a bug .. (: there it is .. !? ---------------- muaaa .. (:

                    A Offline
                    A Offline
                    Alexandru Savescu
                    wrote on last edited by
                    #9

                    Deleting a NULL pointer will do nothing and the original memory never gets freed. Try:

                    delete pWnd;
                    pWnd = NULL;

                    Best regards, Alexandru Savescu

                    1 Reply Last reply
                    0
                    • N Nathan Blomquist

                      Mandalay wrote: _there is my DEL proc :

                      int CMainDlg::DelTab(int Index)
                      {
                      TC_ITEM tci;
                      CChatDlg *pWnd;
                      int iCount = m_tabs.GetItemCount();
                      int iCurrent = m_tabs.GetCurSel();
                      m_tabs.GetItem(Index,&tci);
                      ASSERT(tci.lParam);
                      pWnd = (CChatDlg *)tci.lParam; //CChatDlg is CDialog
                      pWnd->DestroyWindow();
                      pWnd = NULL; // why do you do this?
                      delete pWnd; // this is a null pointer, why delete it?
                      m_tabs.DeleteItem(Index);
                      };

                      Sometimes its works... but i can't found a bug .. (: there it is .. !?_ Why do you set the pointer to NULL before deletion? Perhaps I missed something in another reply to this message... Later, Nathan --------------------------- Hmmm... what's a signature?

                      C Offline
                      C Offline
                      CoY0te
                      wrote on last edited by
                      #10

                      As I said: be patient - inspecting the code I've noticed You reversed the order of two code lines. The proper order is: delete pWnd; pWnd = NULL; ...no comment... ;) PS: Although delete doesn't set the pointer to NULL it is not necessarry to do that unless it's the only information about existance of an object. Greetings to all assembler dudes and especially for C64 demoscene!

                      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