Microsoft KB Archive/822039

= BUG: You receive a run-time exception error message when you run the debug build of your application with the /RTCs compiler option enabled =

Article ID: 822039

Article Last Modified on 6/1/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-





SYMPTOMS
When you run the debug build of your application with the /RTCs compiler option enabled, you may receive the following exception error message:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

You receive this error message when the following conditions are true:
 * You call a function that does not use the __cdecl modifier.
 * The function contains parameters that are larger than 64 kilobytes (KB).
 * You compile your application in the Debug mode and with the /RTCs compiler option enabled.



CAUSE
When you pass a parameter that is 64 KB or larger to a function that does not use the __cdecl modifier, the compiler generates incorrect code. When the function that is called tries to return control to the calling function, the compiler triggers the error that is mentioned in the &quot;Symptoms&quot; section. The compiler generates a 16-bit immediate return (RET) that has an operand that is limited to 64 KB. If the parameters that are passed to the function are larger than 64 KB, the stack becomes corrupted when the function returns.



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to reproduce the behavior
 Start Visual Studio .NET. On the File menu, point to New, and then click Project. The New Project dialog box appears. Under Project Types, click Visual C++ Projects, and then click Win32 Project under Templates. In the Name box, type MyProject, and then click OK. The Win32 Application Wizard - MyProject dialog box appears. On the Welcome to the Win32 Application Wizard page, click Application Settings. The Application Settings page appears. Click Console application, and then click Finish.</li> In Solution Explorer, right-click MyProject.cpp, and then click Open.</li>  Locate the following code in the MyProject.cpp file:  Add the following code after the code that you located in step 8: struct MyStruct {       unsigned char MyData[65535]; };
 * 1) include &quot;stdafx.h&quot; </li>

void __stdcall MyFunc(MyStruct m_MyStruct) {       m_MyStruct.MyData[0] = 'G'; return; } </li>  Replace the existing code in the _tmain procedure with the following code: MyStruct m_Temp; MyFunc(m_Temp); return 0; </li> In Solution Explorer, right-click MyProject, and then click Properties. The MyProject Property Pages dialog box appears.</li> In the left pane of the MyProject Property Pages dialog box, click Configuration Properties.</li> Under Configuration Properties, click C/C++, and then click Code Generation.</li> Set the Basic Runtime Checks property to Stack Frames (/RTCs).</li> Click Configuration Manager. The Configuration Manager dialog box appears.</li> In the Active Solution Configuration list, click Debug, and then click Close.</li> In the MyProject Property Pages dialog box, click OK.</li> On the File menu, click Save All to save all the files.</li> On the Build menu, click Build Solution to build the application.</li> On the Debug menu, click Start to run the application.</li></ol>

<div class="references_section">