corrupt heap while creating objects in a class

Started by
9 comments, last by Fruny 17 years, 10 months ago
Hello, For some reason, i get a corrupt stack error when i try to do ANYTHING to my engine class.

private:
Enumerator* m_pEnum;

IDirect3DDevice9* m_pD3DDevice;
IDirect3D9* m_pD3D;

//D3DPRESENT_PARAMETERS m_D3DWindowedParams; //uncomment for corrupt heap

DEV_OBJECT_CALLBACK m_cbInitDeviceObjects;
DEV_OBJECT_CALLBACK m_cbRestoreDeviceObjects;
DEV_OBJECT_CALLBACK m_cbInvalidateDeviceObjects;
DEV_OBJECT_CALLBACK m_cbDeleteDeviceObjects;

The corrupt heap error typically occurs at either the destructor of the enumerator, the destructor of the this class, or the constructor of the test program that uses this .dll.

HRESULT Engine::Init()
{
	m_pEnum = new Enumerator;
	m_pEnum->SetWindowRect(m_rcClient);

	return S_OK;
};

What the heck is going on? Thanks for any help, exorcist_bob
Advertisement
It's impossible to say from the code you've posted. Most likely, you're passing the address of a pointer to some lock instead of the pointer itself, causing D3D to write over the pointer and anything following it to do the lock properly.

Try commenting out chunks of code (not data) until the problem goes away, then paste the bit that caused the problem to go away. Alternatively, post your entire code here if it's not too huge.
Is this something specifically happening due to your use of DirectX? That is, if you comment out all DX specific code do the problems go away?

Also, depending on your version of VStudio you should get friendly with the debug build options and runtime methods (try pulling up the page on _CrtSetDbgFlag() and navigating around - theres a topic covering all the other similar debug flags). VStudio tends to prefix/suffix special tags to dynamic memory - thus your debugger can tell you what it is you're trampling on.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Quote:Original post by exorcist_bob
What the heck is going on?


Odds are your error looks like this:

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Hello,

I added the code in comments to the engine:

class Engine://Public functionsprivate://D3DPRESENT_PARAMETERS m_D3DWindowedParams;				Enumerator* m_pEnum;IDirect3DDevice9* m_pD3DDevice;IDirect3D9* m_pD3D;DEV_OBJECT_CALLBACK m_cbInitDeviceObjects;DEV_OBJECT_CALLBACK m_cbRestoreDeviceObjects;DEV_OBJECT_CALLBACK m_cbInvalidateDeviceObjects;DEV_OBJECT_CALLBACK m_cbDeleteDeviceObjects;bool m_bRequiresWindowed;bool m_bRequiresFullscreen;bool m_bRequiresHAL;bool m_bRequiresREF;UINT m_intMinFullscreenWidth;UINT m_intMinFullscreenHeight;UINT m_intMinColorChannelBits;UINT m_intMinAlphaChannelBits;UINT m_intMinDepthBits;UINT m_intMinStencilBits;HINSTANCE m_hInst;HWND m_hWnd;RECT m_rcClient;RECT m_rcBounds;}


And I get an error at this place in code:
HRESULT Engine::Init(){	m_pEnum = new Enumerator; //Errors right here	m_pEnum->SetWindowRect(m_rcClient);	return S_OK;};


Error:

Unhandled exception at (Memory address) in test.exe: Microsoft C++ exception: std::bad_alloc at memory location(Memory address).

My only guess is something is up with the constructor of the enumerator class, except nothing abnormal is with it:

Enumerator::Enumerator( void ){	m_pD3D = NULL;	m_rcWindow.top = 400;	m_rcWindow.bottom = 1000;	m_rcWindow.left = 200;	m_rcWindow.right = 1000;	m_cbConfirmDeviceCallback = NULL;    m_pAdapterInfoList = NULL;    m_pAllowedAdapterFormatList = NULL;    m_intMinFullscreenWidth = 640;    m_intMinFullscreenHeight = 480;    m_intMinColorChannelBits = 5;    m_intMinAlphaChannelBits = 0;    m_intMinDepthBits = 15;    m_intMinStencilBits = 0;    	m_bRequiresWindowed = true;    m_bRequiresFullscreen = false;}


Thanks for your help,
exorcist_bob
Quote:Unhandled exception at (Memory address) in test.exe: Microsoft C++ exception: std::bad_alloc at memory location(Memory address).


That means that new failed to allocate the memory. At that point, the constructor hasn't run yet.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Quote:Unhandled exception at (Memory address) in test.exe: Microsoft C++ exception: std::bad_alloc at memory location(Memory address).


That means that new failed to allocate the memory. At that point, the constructor hasn't run yet.


Why would it fail? It can't be because I don't have enough ram(1 GB), can it?
Another thing you could try is either writing your own memory manager, or using MMGR, which might help a little.

EDIT:
Quote:Original post by exorcist_bob
Why would it fail? It can't be because I don't have enough ram(1 GB), can it?
Because your heap is corrupted, as per Fruny's diagram. You actually have around 2GB addressable memory, due to the OS handling virtual memory and all that.
So, how can i get enough memory inbetween the 'sandwich'?
At the beginning and end of each function that you've written, put this code:

assert(_CrtCheckMemory());


This will check the entire heap for corruption. If you put it at the end of each function, you will know which function is corrupting the heap. Then it's a simple matter of looking backwards in that function to see where it's writing more than it should.

MSDN documentation on the function. Note: it'll slow down your program a little, so you might want to take out all the calls once the bug is found and fixed.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement