The code is C++.
Edit: Clarify that the code is C++. Added an ASSERT to indicate which values should be valid for pwszFoo.
Edit: Fixed the assert.
Edit: Clarified the hints.
Edit: Removed the parameters from main to remove distractions from problem.
Hint 1: This code will crash some of the time. Sometimes it will succeed.
Hint 2: This code compiles on Visual Studio. You need to include atlbase.h in your headers.
Hint 3: This bug is quite subtle. I only saw it after staring at it for quite some time. Try stepping through the code in your debugger, line by line. Concentrate on line 6.
Hint 4: This bug is an example of how you can get yourself into trouble when you use object oriented programming without understanding what's going on under the covers.
Code:
class CFoo
{
public:
LPCWSTR GetText()
{
return m_sbstrText ? m_sbstrText : L"";
}
private:
CComBSTR m_sbstrText;
};
int main()
{
LPCWSTR pwszFoo = NULL;
CFoo Foo;
pwszFoo = Foo.GetText();
ASSERT( pwszFoo && wcscmp( pwszFoo, L"" ) == 0 );
return 0;
}
Edit: Clarify that the code is C++. Added an ASSERT to indicate which values should be valid for pwszFoo.
Edit: Fixed the assert.
Edit: Clarified the hints.
Edit: Removed the parameters from main to remove distractions from problem.
Hint 1: This code will crash some of the time. Sometimes it will succeed.
Hint 2: This code compiles on Visual Studio. You need to include atlbase.h in your headers.
Hint 3: This bug is quite subtle. I only saw it after staring at it for quite some time. Try stepping through the code in your debugger, line by line. Concentrate on line 6.
Hint 4: This bug is an example of how you can get yourself into trouble when you use object oriented programming without understanding what's going on under the covers.