CComobox::DrawItem list box entries Disappears In Release Mode
-
Hi I have CBS_SIMPLE CBS_OWNERDRAWVARIABLE ComboBox that I populate via DrawItem using DrawText everything works fine in debug mode and for that matter in release mode while under the Visual Studio Debugger However When I run code in Release not under the debugger the list box entries appear for a instant and then disappear Any Suggestion how to go about debugging would be appreciated
-
Hi I have CBS_SIMPLE CBS_OWNERDRAWVARIABLE ComboBox that I populate via DrawItem using DrawText everything works fine in debug mode and for that matter in release mode while under the Visual Studio Debugger However When I run code in Release not under the debugger the list box entries appear for a instant and then disappear Any Suggestion how to go about debugging would be appreciated
Your code is relying on a local variable value to start zero and it won't be in release (they start unset usually just junk whatever was on the stack beforehand when it gets allocated) but in debug it zeros all local variables. It is one of the core differences between debug and release mode. Usually its something like a bool flag that is assumed to start false (0) which won't in release mode as any non zero value means true. Just check which local variable(s) you are assuming starts as zero and set the thing to zero manually :-)
/* this will behave the same as someGoodFunc in debug but not in release */
void someWrongFunc (void)
{
bool IamaBadBool;
}void someGoodFunc (void)
{
bool IamGoodBool = 0;
}In vino veritas
-
Hi I have CBS_SIMPLE CBS_OWNERDRAWVARIABLE ComboBox that I populate via DrawItem using DrawText everything works fine in debug mode and for that matter in release mode while under the Visual Studio Debugger However When I run code in Release not under the debugger the list box entries appear for a instant and then disappear Any Suggestion how to go about debugging would be appreciated
Have a look at this great Joe Newcomer's essay: [Debug vs. Release](http://www.flounder.com/debug\_release.htm)
-
Your code is relying on a local variable value to start zero and it won't be in release (they start unset usually just junk whatever was on the stack beforehand when it gets allocated) but in debug it zeros all local variables. It is one of the core differences between debug and release mode. Usually its something like a bool flag that is assumed to start false (0) which won't in release mode as any non zero value means true. Just check which local variable(s) you are assuming starts as zero and set the thing to zero manually :-)
/* this will behave the same as someGoodFunc in debug but not in release */
void someWrongFunc (void)
{
bool IamaBadBool;
}void someGoodFunc (void)
{
bool IamGoodBool = 0;
}In vino veritas
the only thing I can come with first is this piece of code I only display part of the string in the list box
void Casidcombo::DrawItem(LPDRAWITEMSTRUCT pdi)
{
ASSERT(pdi->CtlType == ODT_COMBOBOX);
LPCTSTR lpszText = (LPCTSTR)pdi->itemData;ASSERT(lpszText == NULL); CDC dc; CWnd\* phwnd = CWnd::FromHandle(pdi->hwndItem); dc.Attach(pdi->hDC); CString str(lpszText,8); // Get the first 8 charcaters
and later write it out
// Draw the text.
dc.DrawText(
str,
(int)_tcslen(str),
&pdi->rcItem,
DT_CENTER | DT_SINGLELINE | DT_VCENTER);However str is on the stack and would disappear after exiting draw item ? I'll try it out
-
Have a look at this great Joe Newcomer's essay: [Debug vs. Release](http://www.flounder.com/debug\_release.htm)
-
the only thing I can come with first is this piece of code I only display part of the string in the list box
void Casidcombo::DrawItem(LPDRAWITEMSTRUCT pdi)
{
ASSERT(pdi->CtlType == ODT_COMBOBOX);
LPCTSTR lpszText = (LPCTSTR)pdi->itemData;ASSERT(lpszText == NULL); CDC dc; CWnd\* phwnd = CWnd::FromHandle(pdi->hwndItem); dc.Attach(pdi->hDC); CString str(lpszText,8); // Get the first 8 charcaters
and later write it out
// Draw the text.
dc.DrawText(
str,
(int)_tcslen(str),
&pdi->rcItem,
DT_CENTER | DT_SINGLELINE | DT_VCENTER);However str is on the stack and would disappear after exiting draw item ? I'll try it out
Str is on the stack and will go bye bye as soon as you leave the function.
In vino veritas
-
the only thing I can come with first is this piece of code I only display part of the string in the list box
void Casidcombo::DrawItem(LPDRAWITEMSTRUCT pdi)
{
ASSERT(pdi->CtlType == ODT_COMBOBOX);
LPCTSTR lpszText = (LPCTSTR)pdi->itemData;ASSERT(lpszText == NULL); CDC dc; CWnd\* phwnd = CWnd::FromHandle(pdi->hwndItem); dc.Attach(pdi->hDC); CString str(lpszText,8); // Get the first 8 charcaters
and later write it out
// Draw the text.
dc.DrawText(
str,
(int)_tcslen(str),
&pdi->rcItem,
DT_CENTER | DT_SINGLELINE | DT_VCENTER);However str is on the stack and would disappear after exiting draw item ? I'll try it out
ForNow wrote:
dc.Attach(pdi->hDC);
CString str(lpszText,8); // Get the first 8 charcatersand later write it out
// Draw the text.
dc.DrawText(
str,
(int)_tcslen(str),BTW, if you are using CString then you have to use CString::GetLength(), not the C-runtime _tcslen(str).