0

I'm trying to copy a wide c-string from one place into another. I'm using Visual Studio 2012 express on windows8 64-bit platform. It works perfectly fine unless i try to run the application on my main computer with Windows7 x64. It crashes instantly. No exception error though it's a messy crash with no trackable error code whatsoever. If you need more specific information about the crash itself i will try to provide it. When i comment out the copying the program works perfectly fine. So it's pretty obvious the problem is with the function itself. Here is the line that does all the copying:

virtual void CClass::ChangeText();
void CClass::ChangeText(float _f)
{
    std::wstringstream wss;
    wss << _f;
    wcscpy(const_cast<wchar_t *>(this->m_lpszWideText),wss.str().c_str());
}

^ crashes on win7 / works on win8

My wild guess is that the new compiler uses a newer version of wmemcpy that just doesn't work on windows 7? Shouldn't the program crash only when it reaches the function call line though?

  • 2
    How is `m_lpszWideText` defined? – hmjd Apr 15 '13 at 14:40
  • It's my own (text-component) class' member pointer: `private: const wchar_t* m_lpszWideText;` – user1756573 Apr 15 '13 at 14:47
  • 1
    How do you allocate memory for it? Why is it `const` when it requires modification? Why not just use `std::wstring`? – hmjd Apr 15 '13 at 14:49
  • actually that's not a bad idea.. oops. Well i was always taught that c-string is a "const char*" no matter what. I'm still not sure if it's a constant pointer to a char array or a pointer to an array of const chars rofl. – user1756573 Apr 15 '13 at 15:03

1 Answers1

0

A Crash with String-Copy algorithms have usually two origins:

  1. Your Source is not NULL-Terminated In Your Example this is not the case, because you extract it from wstringstream::c_str()

  2. Your Destination is not big enough to handle the source data, and so is written out of Bounds. This may be the cause of your Crash, means, your this->m_lpszWideText is too small (please give the Declaration of it and, if it is dynamically allocated show us, how.)

EGOrecords
  • 1,959
  • 2
  • 19
  • 33
  • Impossible, otherwise it would crash on every platform. It is now a `wchar_t WideText [150];` and i initially assign it a value through wcscpy(). It still crashes on the other computer and works fine on win8... – user1756573 Apr 15 '13 at 15:04
  • i assign it a value of 100 through the function ChangeText for which i pasted code in my previous post. – user1756573 Apr 15 '13 at 15:07
  • Maybe Win8 has another Memory Management than Win7, wich means, that you can write out of your memory, without writing into a protected area, while in win7 you write out of the area. Is it maybe a 64/32 Bit issue? Check the size of the string to copy befor copying it, if it fits? – EGOrecords Apr 15 '13 at 15:48
  • That's very plausible. Solution for this situation: Stick to wstring except for the moment when you need to extract and pass c-string -> use wstringstream::c_str(). – user1756573 Apr 15 '13 at 16:36