PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Pocket PC string/memory handling

stickyfingers · 29 Jun 2004, 19:04 UTC
I have developed a label widget which I use in my upcomming game. The label has a SetCaption function which is set regurlarly in my mainloop.
The following code works fine on PC, but on PPC it simply crash.

Code:

void CGuiLabel::SetCaption(TCHAR* text)
{
if (text != NULL)
{
if (caption != NULL)
free(caption);

int length = _tcslen(text);
caption = (TCHAR*)malloc(length);
_stprintf(caption, text);
}
}


Never mind that this could have been optimized a lot. I just want it to work :roll:
If I substitute
Code:
text
with
Code:
TEXT("MyCaption");
inside the function it works fine.

Anyone out there got an idea what's going on?

-Thomas
You have to use the TEXT("") and all the _T macros while using the TCHAR stuff to make your code compile & run on both PC & PocketPC. Using these features allows ascii on pc & unicode on PPC without doing any extra work. Why don't you want to use the TEXT("") approach
Line 9 is incorrect - you aren't allocating enough bytes. You aren't allowing for the null terminator, and for Unicode builds a TCHAR is 2 bytes. So you are allocating length+2 too few bytes.

Code:

caption = (TCHAR*)malloc((length+1)*sizeof(TCHAR));


That will work for unicode or ansi builds.

Also I'm not sure why you're using _sprintf instead of _wcscpy. If text contains any formatting codes like %d then _sprintf will try to access arguments that it wasn't passed and your app will crash. Plus _wcscpy is faster than _sprintf.

Dan East
stickyfingers · 29 Jun 2004, 20:23 UTC
Conan

I already use the TEXT("") approach. I call the SetCaption function like this:
Code:
myLabel->SetCaption(TEXT("This is myLabel"));
but it still crash. I just don't get it why it crash in SetCaption?
in the line
Code:
free(caption);
to be specific. Should I be using the TEXT approach inside the function? if so - where???


-Thomas
In CGuiLabel::CGuiLabel() are you setting caption to NULL?

Dan East
stickyfingers · 29 Jun 2004, 20:31 UTC
Dan

Yes, I initially set caption to NULL in the constructor
The malloc fix works like a charm
Thanks a bunch!! :D