Jump to content

  • Log In with Google      Sign In   
  • Create Account


Run-Time Check Failure #2


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 monkeyboi   Members   -  Reputation: 188

Like
0Likes
Like

Posted 18 July 2012 - 08:00 AM

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, 18 July 2012 - 08:02 AM.


Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1148

Like
0Likes
Like

Posted 18 July 2012 - 09:10 PM

A good place to start would be MeshDemo::run, since that is apparently where the problem is.

#3 Hodgman   Moderators   -  Reputation: 27622

Like
2Likes
Like

Posted 18 July 2012 - 09:36 PM

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.

#4 monkeyboi   Members   -  Reputation: 188

Like
1Likes
Like

Posted 19 July 2012 - 06:43 AM

Thanks Hodgman

Yeah that is the place where goes wrong. I will be more careful next time.

Jerry




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS