Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I'm still learning so forgive another noob problem.
I'll abreviate the code to around the area that has given me grief.

All I'm trying to do is get a string, slap a number onto the end of it, then display it. The string arrives on the screen just as planned, but I get the Stack Corruption at the end of the function as it returns control to WndProc.

#include <windows.h>
#include <stdlib.h>

void FailingFunction(HWND hwnd)
{
   HDC hdc = GetDC(hwnd);
   RECT TextRect1;

   int aNumber = 10;
   wchar_T ident[] = L"A string";
   wchar_T Out[50];

   TextRect1.top = 15;
   TextRect1.left = 15;
   TextRect1.bottom = 100;
   TextRect1.right = 100;

   PrepTextOut(ident, Out, aNumber);

   DrawText(hdc, Out, -1, &TextRect1, DT_SINGLELINE | DT_CENTER | DT_VCENTER)

   ReleaseDC(hwnd,hdc);
}

void PrepTextOut(wchar_t* str, wchar_t* dest, int value)
{
   wchar_t cNum[10]; //Wide character array to hold integer
   size_t cNumSize = 10; //size of 'wchar_t cNum' 
   size_t destSize = wcslen(dest); //size of dest wide character array
   _itow_s(value, cNum, cNumSize, 10); //Convert cNum integer to wchar_t
   wcscpy_s(dest, destSize, str); //Copy str into dest
   wcscat_s(dest, destSize, cNum); //Contracate cNum onto dest
}</stdlib.h></windows.h>
I need to do this step dozens of times and was looking to shorten the code. I've included all of the necessities to make it run. My test integer was 1. Test string was 'Strength'.

Any help is greatly appreciated.
Posted
Comments
Sergey Alexandrovich Kryukov 18-Mar-11 20:01pm    
I see no signs of stack corruption. You need to report the problem properly. I there is an exception, catch it, dump in in full in Debug configuration, examine the stack and mark mark the code line where exception is thrown/propagated. Post this information. Use "Improve question". (Please, don't post it as Answer.)
Why "another noob"? This is your first Question, isn' it?
--SA

Stack corruption may occur by a 10 digit integer value because there is no space for terminating null character. wcslen(dest); call may cause access violation on uninitialized dest buffer while looking for a terminating null to determine the end of string.

My recommendations have been stated as comment lines below.
//void PrepTextOut(wchar_t* str, wchar_t* dest, int value)
void PrepTextOut(wchar_t* str, wchar_t* dest, size_t destSize, int value)
// needs one more parameter for destsize
{
  //wchar_t cNum[10]; //Wide character array to hold integer
  // 10 is not enough (together with terminator null and posible minus sign,
  // should be at least 12 for 32 bit integers)
  wchar_t cNum[16]; // guarantee

  // size_t cNumSize = 10; // size of 'wchar_t cNum'
  size_t cNumSize = sizeof(cNum) / sizeof(cNum[0]);

  // size_t destSize = wcslen(dest); //size of dest wide character array
  // dest may points uninitalized buffer so you shouldn't do above

  _itow_s(value, cNum, cNumSize, 10); //Convert cNum integer to wchar_t

  wcscpy_s(dest, destSize, str); //Copy str into dest
  wcscat_s(dest, destSize, cNum); //Contracate cNum onto dest
}
 
Share this answer
 
Could it be this:

size_t destSize = wcslen(dest); //size of dest wide character array

You're using "destSize" later, but I don't think this value is being determined correctly. From "FailingFunction", you're passing "wchar_T out[50]" as "dest". But it's just filled with 50 wchar's of stack junk, whatever happens to be there at the time. So the result of size_t destSize = wcslen(dest) is undetermined.

Maybe try initializing as follows:

wchar_T Out[50] = L"";

Or try passing "destSize" (in this case 50) as a parameter instead of trying to determine it via wcslen.

- Bob
 
Share this answer
 
Setting to 50 right off the bat worked

wcscpy_s(dest, 50, str); //Copy base into dest
wcscat_s(dest, 50, cNum); //Contracate cNum onto dest

I'll remember to get the size of the array first and passing that value to the function.

Thanks for the advice for cNum, I'll set that to cNum[32] or possibly cNum[64] just to be safe.

And this is my second question, my first was really, really noob (confused sizeof with the literal translation 'size of'). It's a good thing that I'm a quick study.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900