Jump to content
  • Advertisement
Sign in to follow this  
monkeyboi

Run-Time Check Failure #2

This topic is 2254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

All I want to do right now is measuring the time consumed by a certain call. Because the original program is not written by me, so I only modify very a few places. I will give you the structure first

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
PSTR cmdLine, int showCmd)
{
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
MeshDemo app(hInstance, "Mesh Demo", D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING);
gd3dApp = &app;
DirectInput di(DISCL_NONEXCLUSIVE|DISCL_FOREGROUND, DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
gDInput = &di;
if(!gd3dApp->checkDeviceCaps())
return 0;
else
return gd3dApp->run(); //corrupted here
}



//global scope//
//double preTime;
//double curTime;
//float secsPerCnt;
//double timePass;
//global scope//

class MeshDemo : public D3DApp
{
public:
MeshDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP);
~MeshDemo();
...
private:
...
//Time measurement
D3DXFONT_DESC d3dFont;
ID3DXFont* m_pFont;
double preTime;
double curTime;
float secsPerCnt;
double timePass;
};


void MeshDemo::SetupFond(){
memset(&d3dFont,0,sizeof(d3dFont));
d3dFont.Height=15; // in logical units
d3dFont.Width=7; // in logical units
d3dFont.Weight=500;// boldness, range 0(light) - 1000(bold)
d3dFont.Italic=FALSE;
d3dFont.CharSet=DEFAULT_CHARSET;
memcpy(d3dFont.FaceName,"Times New Roman",64);
//d3dFont.FaceName = *L"Times New Roman";
D3DXCreateFontIndirect(gd3dDevice, &d3dFont, &m_pFont);
long long cntsPerSec = 0;
QueryPerformanceFrequency((LARGE_INTEGER*)&cntsPerSec);
secsPerCnt = 1.0f / (float)cntsPerSec;
long long CurTimeStamp = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&CurTimeStamp);
preTime = secsPerCnt * CurTimeStamp;
curTime = preTime;
timePass = 0;
}


void MeshDemo::drawScene()
{
long long CurTimeStamp = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&CurTimeStamp);
curTime = secsPerCnt * CurTimeStamp;
timePass = curTime - preTime;
preTime = curTime;
// Clear the backbuffer and depth buffer.
HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));
HR(gd3dDevice->BeginScene());
RECT Rect = { 0,0,600,600 };
char* temp = new char[100];
sprintf(temp,"Time Since Last Frame = %f\n",timePass);
m_pFont->DrawText(NULL,(LPCSTR)temp, -1, &Rect, DT_CENTER | DT_VCENTER, 0xFFFF0000);
delete[] temp;

HR(gd3dDevice->EndScene());
// Present the backbuffer.
HR(gd3dDevice->Present(0, 0, 0, 0));
}



The above version works fine, but when i move the variables, including preTime, curTime, secsPerCnt and timePass, to globle scope, I will get Run-Time Check Failure #2 - Stack around the variable 'app' was corrupted assertion every time I shut the program. And the program will stop and point to return gd3dApp->run() where is in the main function.

Any idea about the reason or how to track it down?

Jerry Edited by monkeyboi

Share this post


Link to post
Share on other sites
Advertisement
This looks very dodgy:memcpy(d3dFont.FaceName,"Times New Roman",64);
FaceName is LF_FACESIZE (IIRC, 32) char's long, but you're copying 64 bytes of data into it. That means, you're corrupting the 32 bytes of memory that are allocated immediately after that variable, which in your case is:ID3DXFont* m_pFont;
double preTime;
double curTime;
float secsPerCnt;
double timePass;
So when you remove those variables from your class, then instead of just corrupting your own memory with that faulty memcpy, you're trashing someone elses memory (which turns out to be stack guards).

Furthermore, with that memcpy, your source array is only 16-bytes long, so reading 64-bytes from could cause a fatal error at that point (if trying to read protected memory).
You should calculate how many bytes are in your source array ("Times New Roman"), and then assert that this size is less than or equal to LF_FACESIZE, and then perform the copy operation using this measured size.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!