Sign in to follow this  
exorcist_bob

corrupt heap while creating objects in a class

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by exorcist_bob
What the heck is going on?


Odds are your error looks like this:

Share this post


Link to post
Share on other sites
Hello,

I added the code in comments to the engine:


class Engine:
//Public functions
private:
//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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this