Heap corruption.
-
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#
-
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#
The biggest difference is that
new
calls the constructor for the object being allocated, whilemalloc()
only allocates memory. Even though you may not define one, a simplestruct
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 toNULL
). Withmalloc()
, that initialization isn't happening.
Software Zen:
delete this;
-
The biggest difference is that
new
calls the constructor for the object being allocated, whilemalloc()
only allocates memory. Even though you may not define one, a simplestruct
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 toNULL
). Withmalloc()
, that initialization isn't happening.
Software Zen:
delete this;
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#
-
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#
One more thing, The GlobalAlloc() and GlobalFree() also act the similar way as the malloc() does.
Polite Programmer
More Object Oriented then C#
-
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#
Hmm. Myself, I always prefer the
new
/delete
approach overmalloc()
/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 usingmalloc()
?
Software Zen:
delete this;
-
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#
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
anddelete
. The Debug CRTnew
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 thedelete,
it checks this info to make sure it matches but because you calledmalloc
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 matchmalloc
withfree
andnew
withdelete
;)Nothing is exactly what it seems but everything with seems can be unpicked.
-
Hmm. Myself, I always prefer the
new
/delete
approach overmalloc()
/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 usingmalloc()
?
Software Zen:
delete this;
-
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#
-
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#
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
-
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
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)
-
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)
Ooooooopsssss! Sorry for that much long message....
Polite Programmer
More Object Oriented then C#
-
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)
Follow the steps I gave and odds are the dubugger will take you straight to the source of the problem.
Steve