Jump to content
  • Advertisement
Sign in to follow this  

Run-Time Check Failure #2

This topic is 2164 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)
gd3dApp = &app;
gDInput = &di;
return 0;
return gd3dApp->run(); //corrupted here

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

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

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

void MeshDemo::drawScene()
long long CurTimeStamp = 0;
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));
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;

// 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
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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!