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. Heap corruption.

Heap corruption.

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpdebuggingoopquestion
12 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.
  • P Offline
    P Offline
    Polite Programmer
    wrote on last edited by
    #1

    I am writting a button control from scratch in pure C + Win32. I have a structure as under typedef struct tagBUTTONINFO { TCHAR * szText; int iLenght; COLORREF backColor // Other memebers... } BUTTONINFO, * PBUTTONINFO; Now on each WM_NCCREATE, I use malloc() to allocate a BUTTONINFO and set its address in USERDATA of the HWND of the window. In each WM_SETTEXT, I allocate space for the text and set in the PBUTTONINFO->szText. On WM_NCDESTROY, I deallocate the space for BUTTONINFO and the BUTTONINFO::szText as well. But in debug sessions, I get Heap Corruption Error Message. Whats wrong? However, if you use new and delete keywords for the same tasks, no such problem occurs... whats wrong and where?

    Polite Programmer


    More Object Oriented then C#

    G M C S 4 Replies Last reply
    0
    • P Polite Programmer

      I am writting a button control from scratch in pure C + Win32. I have a structure as under typedef struct tagBUTTONINFO { TCHAR * szText; int iLenght; COLORREF backColor // Other memebers... } BUTTONINFO, * PBUTTONINFO; Now on each WM_NCCREATE, I use malloc() to allocate a BUTTONINFO and set its address in USERDATA of the HWND of the window. In each WM_SETTEXT, I allocate space for the text and set in the PBUTTONINFO->szText. On WM_NCDESTROY, I deallocate the space for BUTTONINFO and the BUTTONINFO::szText as well. But in debug sessions, I get Heap Corruption Error Message. Whats wrong? However, if you use new and delete keywords for the same tasks, no such problem occurs... whats wrong and where?

      Polite Programmer


      More Object Oriented then C#

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      The biggest difference is that new calls the constructor for the object being allocated, while malloc() only allocates memory. Even though you may not define one, a simple struct has a default constructor provided by the compiler. I believe the default constructor under debug initializes the allocated memory to zero. This is probably setting your structure to a 'good' initial state (pointers get initialized to NULL). With malloc(), that initialization isn't happening.


      Software Zen: delete this;

      Fold With Us![^]

      P 1 Reply Last reply
      0
      • G Gary R Wheeler

        The biggest difference is that new calls the constructor for the object being allocated, while malloc() only allocates memory. Even though you may not define one, a simple struct has a default constructor provided by the compiler. I believe the default constructor under debug initializes the allocated memory to zero. This is probably setting your structure to a 'good' initial state (pointers get initialized to NULL). With malloc(), that initialization isn't happening.


        Software Zen: delete this;

        Fold With Us![^]

        P Offline
        P Offline
        Polite Programmer
        wrote on last edited by
        #3

        But the constructor is not needed to dot that because after executing the malloc(), I immediatly call ZeroMemory like this. PBUTTONINFO pButtonInfo = (PBUTTONINFO) malloc (sizeof (BUTTONINFO)); ZeroMemory ((LPVOID) pButtonInfo, sizeof (BUTTONINFO));

        Polite Programmer


        More Object Oriented then C#

        P G 2 Replies Last reply
        0
        • P Polite Programmer

          But the constructor is not needed to dot that because after executing the malloc(), I immediatly call ZeroMemory like this. PBUTTONINFO pButtonInfo = (PBUTTONINFO) malloc (sizeof (BUTTONINFO)); ZeroMemory ((LPVOID) pButtonInfo, sizeof (BUTTONINFO));

          Polite Programmer


          More Object Oriented then C#

          P Offline
          P Offline
          Polite Programmer
          wrote on last edited by
          #4

          One more thing, The GlobalAlloc() and GlobalFree() also act the similar way as the malloc() does.

          Polite Programmer


          More Object Oriented then C#

          1 Reply Last reply
          0
          • P Polite Programmer

            But the constructor is not needed to dot that because after executing the malloc(), I immediatly call ZeroMemory like this. PBUTTONINFO pButtonInfo = (PBUTTONINFO) malloc (sizeof (BUTTONINFO)); ZeroMemory ((LPVOID) pButtonInfo, sizeof (BUTTONINFO));

            Polite Programmer


            More Object Oriented then C#

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            Hmm. Myself, I always prefer the new/delete approach over malloc()/free(), because these sorts of issues don't tend to arise as often. There isn't a significant difference between the performance of the two approaches. Is there any particular reason you're using malloc()?


            Software Zen: delete this;

            Fold With Us![^]

            C 1 Reply Last reply
            0
            • P Polite Programmer

              I am writting a button control from scratch in pure C + Win32. I have a structure as under typedef struct tagBUTTONINFO { TCHAR * szText; int iLenght; COLORREF backColor // Other memebers... } BUTTONINFO, * PBUTTONINFO; Now on each WM_NCCREATE, I use malloc() to allocate a BUTTONINFO and set its address in USERDATA of the HWND of the window. In each WM_SETTEXT, I allocate space for the text and set in the PBUTTONINFO->szText. On WM_NCDESTROY, I deallocate the space for BUTTONINFO and the BUTTONINFO::szText as well. But in debug sessions, I get Heap Corruption Error Message. Whats wrong? However, if you use new and delete keywords for the same tasks, no such problem occurs... whats wrong and where?

              Polite Programmer


              More Object Oriented then C#

              M Offline
              M Offline
              Matthew Faithfull
              wrote on last edited by
              #6

              You'll get those heap corruption messages in Debug only because in debug the CRT ( C RunTime library ) is checking up on you and picking up the mismatch between malloc and delete. The Debug CRT new adds extra info to the allocation to indicate what sort of allocation it is, i.e. you did it or it's internal to the CRT or thread specific or whatever. When you do the delete, it checks this info to make sure it matches but because you called malloc the extra info didn't get initialised and it's either wrong or missing. In Release the extra info and checking is cut for performance so you don't get the errors. Conclusion: For a happy life you really do need to match malloc with free and new with delete ;)

              Nothing is exactly what it seems but everything with seems can be unpicked.

              1 Reply Last reply
              0
              • G Gary R Wheeler

                Hmm. Myself, I always prefer the new/delete approach over malloc()/free(), because these sorts of issues don't tend to arise as often. There isn't a significant difference between the performance of the two approaches. Is there any particular reason you're using malloc()?


                Software Zen: delete this;

                Fold With Us![^]

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Because he wants to use pure C.

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                1 Reply Last reply
                0
                • P Polite Programmer

                  I am writting a button control from scratch in pure C + Win32. I have a structure as under typedef struct tagBUTTONINFO { TCHAR * szText; int iLenght; COLORREF backColor // Other memebers... } BUTTONINFO, * PBUTTONINFO; Now on each WM_NCCREATE, I use malloc() to allocate a BUTTONINFO and set its address in USERDATA of the HWND of the window. In each WM_SETTEXT, I allocate space for the text and set in the PBUTTONINFO->szText. On WM_NCDESTROY, I deallocate the space for BUTTONINFO and the BUTTONINFO::szText as well. But in debug sessions, I get Heap Corruption Error Message. Whats wrong? However, if you use new and delete keywords for the same tasks, no such problem occurs... whats wrong and where?

                  Polite Programmer


                  More Object Oriented then C#

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Try to post the relevant code. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                  1 Reply Last reply
                  0
                  • P Polite Programmer

                    I am writting a button control from scratch in pure C + Win32. I have a structure as under typedef struct tagBUTTONINFO { TCHAR * szText; int iLenght; COLORREF backColor // Other memebers... } BUTTONINFO, * PBUTTONINFO; Now on each WM_NCCREATE, I use malloc() to allocate a BUTTONINFO and set its address in USERDATA of the HWND of the window. In each WM_SETTEXT, I allocate space for the text and set in the PBUTTONINFO->szText. On WM_NCDESTROY, I deallocate the space for BUTTONINFO and the BUTTONINFO::szText as well. But in debug sessions, I get Heap Corruption Error Message. Whats wrong? However, if you use new and delete keywords for the same tasks, no such problem occurs... whats wrong and where?

                    Polite Programmer


                    More Object Oriented then C#

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #9

                    I give the following advice about once a month (more recently); it often helps me track down the nastier heap errors: Try enabling the page heap[^] for your process. Follow these steps: 1. Download and install WinDBG[^]. 2. Select “Start”->“All Programs”->“Debugging Tools for Windows”->“Global Flags”. 3. Select the “Image File” tab. 4. In the “Image: (TAB to refresh)” edit control enter the name of your app then press TAB. Just the name with the extension; not the full path. 5. Tick the following: - “Enable page heap” - “Enable heap tail checking” - “Enable heap free checking” - “Enable heap parameter checking” - “Enable heap validation on call” - “Create user mode stack trace database” 6. Press “Apply”. 7. Debug your application. Any debugger will do but with WinDBG you have access to the stack traces of allocations via the !heap –p –a command, for example. When a heap problem is detected a breakpoint will be generated. 8. When done un-tick all the options you ticked, press “Apply” then dismiss GFlags. This step is important as if it’s skipped all applications named as entered in step 4 will run with the page heap enabled. Note that when using the page heap your application will run much slower than normal and consume way more memory. It’s good to have a beefy machine to do such tests; and such tests should be ran regularly on all applications you develop as part of regular testing activities. If I find a part of my application that’s too slow with the page heap enabled I optimize the memory allocation in that region.

                    Steve

                    P 1 Reply Last reply
                    0
                    • S Stephen Hewitt

                      I give the following advice about once a month (more recently); it often helps me track down the nastier heap errors: Try enabling the page heap[^] for your process. Follow these steps: 1. Download and install WinDBG[^]. 2. Select “Start”->“All Programs”->“Debugging Tools for Windows”->“Global Flags”. 3. Select the “Image File” tab. 4. In the “Image: (TAB to refresh)” edit control enter the name of your app then press TAB. Just the name with the extension; not the full path. 5. Tick the following: - “Enable page heap” - “Enable heap tail checking” - “Enable heap free checking” - “Enable heap parameter checking” - “Enable heap validation on call” - “Create user mode stack trace database” 6. Press “Apply”. 7. Debug your application. Any debugger will do but with WinDBG you have access to the stack traces of allocations via the !heap –p –a command, for example. When a heap problem is detected a breakpoint will be generated. 8. When done un-tick all the options you ticked, press “Apply” then dismiss GFlags. This step is important as if it’s skipped all applications named as entered in step 4 will run with the page heap enabled. Note that when using the page heap your application will run much slower than normal and consume way more memory. It’s good to have a beefy machine to do such tests; and such tests should be ran regularly on all applications you develop as part of regular testing activities. If I find a part of my application that’s too slow with the page heap enabled I optimize the memory allocation in that region.

                      Steve

                      P Offline
                      P Offline
                      Polite Programmer
                      wrote on last edited by
                      #10

                      Reason for using malloc() stuff is that I want to be pure in C. What I am doing? 1.... I want to write a pure C control to teach myself internals of Win32 Message Architecture. 2... When done with this, I will burry that control (a simple button control yet...) into a DLL. 3... When done with the DLL, I will try to write a COM wrapper around it that too in pure C to check whether it can be used with VB, and .NET. Friends one more thing... my every malloc() is having a free() but still facing problem. And one of our felllows is very much right, the problem only occurs in Debug sessions. Here is the source code. Please consider the following functions specially.... Are they correct? a) AllocateButtonInfoFor() b) DeleteButtonInfo() c) OnNCCreate() d) OnSetText() First is the ButtonControl.h ============================ #ifndef __BUTTON_CONTROL_H__ #define __BUTTON_CONTROL_H__ #include "stdafx.h" #define BUTTON_CONTROL_CLASS TEXT("BUTTON_CONTROL_safsf") #ifdef __INCLUDE_BUTTONCONTROL_PRIVATES__ /* Depicts the state of the button */ typedef enum tagBUTTONSTATE { NORMAL = 2, HOVER, PRESSED, DISABLED } BUTTONSTATE; /* Structure to hold the information of the button*/ typedef struct tagBUTTONINFO { // HWND hWnd; /* Handle of the button itself */ TCHAR * szText; /* The caption of button */ int nTextLength; /* Length of caption */ HPEN borderColor; /* The outline color */ COLORREF textColor; /* The Text Color of the button */ HBRUSH backColor; /* The background color of the button */ HFONT font; /* The font of the text */ HPEN borderColorPressed; /* The border color in pressed form*/ COLORREF textColorPressed; /* The text color in pressed form */ HBRUSH backColorPressed; /* The back color in pressed form */ HFONT fontPressed; /* The font in pressed form */ HPEN borderColorHover; /* The border color in hover form*/ COLORREF textColorHover; /* The text color in hover form */ HBRUSH backColorHover; /* The back color in hover form */ HFONT fontHover; /* The font in hover form */ BUTTONSTATE state; /* The state of the button */ } BUTTONINFO, * PBUTTONINFO; /* ButtonInfo Allocation */ //HANDLE CreateHeapIfNeeded(); //BOOL DestroyHeapIfNeeded(HANDLE); PBUTTONINFO AllocateButtonInfoFor(HWND hWnd); PBUTTONINFO PickButtonInfo(HWND hWnd); BOOL DropButtonInfo(HWND hWnd); void DeleteButtonInfo(HWND hWnd); void SetButtonDefaults(HWND hWnd, PBUTTONINFO pButtonInfo)

                      P S 2 Replies Last reply
                      0
                      • P Polite Programmer

                        Reason for using malloc() stuff is that I want to be pure in C. What I am doing? 1.... I want to write a pure C control to teach myself internals of Win32 Message Architecture. 2... When done with this, I will burry that control (a simple button control yet...) into a DLL. 3... When done with the DLL, I will try to write a COM wrapper around it that too in pure C to check whether it can be used with VB, and .NET. Friends one more thing... my every malloc() is having a free() but still facing problem. And one of our felllows is very much right, the problem only occurs in Debug sessions. Here is the source code. Please consider the following functions specially.... Are they correct? a) AllocateButtonInfoFor() b) DeleteButtonInfo() c) OnNCCreate() d) OnSetText() First is the ButtonControl.h ============================ #ifndef __BUTTON_CONTROL_H__ #define __BUTTON_CONTROL_H__ #include "stdafx.h" #define BUTTON_CONTROL_CLASS TEXT("BUTTON_CONTROL_safsf") #ifdef __INCLUDE_BUTTONCONTROL_PRIVATES__ /* Depicts the state of the button */ typedef enum tagBUTTONSTATE { NORMAL = 2, HOVER, PRESSED, DISABLED } BUTTONSTATE; /* Structure to hold the information of the button*/ typedef struct tagBUTTONINFO { // HWND hWnd; /* Handle of the button itself */ TCHAR * szText; /* The caption of button */ int nTextLength; /* Length of caption */ HPEN borderColor; /* The outline color */ COLORREF textColor; /* The Text Color of the button */ HBRUSH backColor; /* The background color of the button */ HFONT font; /* The font of the text */ HPEN borderColorPressed; /* The border color in pressed form*/ COLORREF textColorPressed; /* The text color in pressed form */ HBRUSH backColorPressed; /* The back color in pressed form */ HFONT fontPressed; /* The font in pressed form */ HPEN borderColorHover; /* The border color in hover form*/ COLORREF textColorHover; /* The text color in hover form */ HBRUSH backColorHover; /* The back color in hover form */ HFONT fontHover; /* The font in hover form */ BUTTONSTATE state; /* The state of the button */ } BUTTONINFO, * PBUTTONINFO; /* ButtonInfo Allocation */ //HANDLE CreateHeapIfNeeded(); //BOOL DestroyHeapIfNeeded(HANDLE); PBUTTONINFO AllocateButtonInfoFor(HWND hWnd); PBUTTONINFO PickButtonInfo(HWND hWnd); BOOL DropButtonInfo(HWND hWnd); void DeleteButtonInfo(HWND hWnd); void SetButtonDefaults(HWND hWnd, PBUTTONINFO pButtonInfo)

                        P Offline
                        P Offline
                        Polite Programmer
                        wrote on last edited by
                        #11

                        Ooooooopsssss! Sorry for that much long message....

                        Polite Programmer


                        More Object Oriented then C#

                        1 Reply Last reply
                        0
                        • P Polite Programmer

                          Reason for using malloc() stuff is that I want to be pure in C. What I am doing? 1.... I want to write a pure C control to teach myself internals of Win32 Message Architecture. 2... When done with this, I will burry that control (a simple button control yet...) into a DLL. 3... When done with the DLL, I will try to write a COM wrapper around it that too in pure C to check whether it can be used with VB, and .NET. Friends one more thing... my every malloc() is having a free() but still facing problem. And one of our felllows is very much right, the problem only occurs in Debug sessions. Here is the source code. Please consider the following functions specially.... Are they correct? a) AllocateButtonInfoFor() b) DeleteButtonInfo() c) OnNCCreate() d) OnSetText() First is the ButtonControl.h ============================ #ifndef __BUTTON_CONTROL_H__ #define __BUTTON_CONTROL_H__ #include "stdafx.h" #define BUTTON_CONTROL_CLASS TEXT("BUTTON_CONTROL_safsf") #ifdef __INCLUDE_BUTTONCONTROL_PRIVATES__ /* Depicts the state of the button */ typedef enum tagBUTTONSTATE { NORMAL = 2, HOVER, PRESSED, DISABLED } BUTTONSTATE; /* Structure to hold the information of the button*/ typedef struct tagBUTTONINFO { // HWND hWnd; /* Handle of the button itself */ TCHAR * szText; /* The caption of button */ int nTextLength; /* Length of caption */ HPEN borderColor; /* The outline color */ COLORREF textColor; /* The Text Color of the button */ HBRUSH backColor; /* The background color of the button */ HFONT font; /* The font of the text */ HPEN borderColorPressed; /* The border color in pressed form*/ COLORREF textColorPressed; /* The text color in pressed form */ HBRUSH backColorPressed; /* The back color in pressed form */ HFONT fontPressed; /* The font in pressed form */ HPEN borderColorHover; /* The border color in hover form*/ COLORREF textColorHover; /* The text color in hover form */ HBRUSH backColorHover; /* The back color in hover form */ HFONT fontHover; /* The font in hover form */ BUTTONSTATE state; /* The state of the button */ } BUTTONINFO, * PBUTTONINFO; /* ButtonInfo Allocation */ //HANDLE CreateHeapIfNeeded(); //BOOL DestroyHeapIfNeeded(HANDLE); PBUTTONINFO AllocateButtonInfoFor(HWND hWnd); PBUTTONINFO PickButtonInfo(HWND hWnd); BOOL DropButtonInfo(HWND hWnd); void DeleteButtonInfo(HWND hWnd); void SetButtonDefaults(HWND hWnd, PBUTTONINFO pButtonInfo)

                          S Offline
                          S Offline
                          Stephen Hewitt
                          wrote on last edited by
                          #12

                          Follow the steps I gave and odds are the dubugger will take you straight to the source of the problem.

                          Steve

                          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