NEVER store string pointers in Cpp SDK

Date : Fri, 24 Mar 2006 15:17:31 +0300
To : <3DGames(at)Softimage.COM>
From : "Wessam Bahnassi" <wbahnassi(at)inframez.com>
Subject : NEVER store string pointers in Cpp SDK
Hi all,

After spending a couple of days trying to pinpoint a damn memory corruption
bug in our XSI plug-in, I found that it's all caused by a casual use of
XSI::CString class...

Look at this code snip:

XSI::Parameter param; // Assume this is a valid XSI param object...
const char *pszString = param.GetName().GetAsciiString();
printf(pszString); // Potential crash!

What happens is that param.GetName() returns a temp XSI::CString object. And
after the call to GetAsciiString(), this temp string object is destroyed,
rendering the pointer pszString potentially invalid...
Of course in our code we do a lot more stuff instead of printf(), and that
put me in endless wonders why many of the strings I receive are garbage!

I hope this saves some poor souls out there as I don't like to see people
suffering from this mistake...

BTW, I found a similar line in the SDK sample dotXSIConverter, file
cnv_model.cpp:
const wchar_t *l_pName = l_XSIModel.GetName().GetWideString();

Fortunately the pointer returned isn't used anywhere, so no physical harm is
done :)

(perhaps this should be added to the Softimage wiki?)

Thanks,
Wessam Bahnassi
Microsoft DirectX MVP,
Lead Programmer
In|Framez
--
In|Structurez Arabic Gamedev Community
www.instructurez.com

---
Unsubscribe? Mail Majordomo(at)Softimage.COM with the following text in body:
unsubscribe 3dgames


Search the 3D Games List archives here or use the advanced search form to search across mailing lists. Searching help is available.
This site supposedly brought to you by Benjamin Grosser and the Imaging Technology Group.