Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CComboBox::GetLBText returns garbage

Tags:

c++

mfc

I am filling a combobox:

    while((pHPSet = pHPTable->GetNext()) != NULL)
    {       
      CString str = pHPSet->GetName();
          // I am normally using str but to proove that this is 
          // not the problem I am using "a"
      m_comboBaseHP.AddString(_T("a"));
    }

Now I am trying to read the combobox:

if(m_comboBaseHP.GetCount() > 0)
{
    CString csHPName = _T("");
    m_comboBaseHP.GetLBText(0, csHPName);
    // This is the ms way but ReleaseBuffer causes a crash
    //CString str = _T("");
    //int n = m_comboBaseHP.GetLBTextLen( 0 );
    //m_comboBaseHP.GetLBText( 0, str.GetBuffer(n) );
    //str.ReleaseBuffer();

    // Do whatever with csHPName
}

The problem is that csHPName shows in the Debugger some Chinese signs. I am assuming this is memory garbage. This happens in the same Method. This happens pre draw. Post draw the same issue. This happens in Debug and Release. I don't understand how this can happen since I am not actually working with pointers.

like image 347
Martin Avatar asked Oct 26 '25 17:10

Martin


1 Answers

Apparently it is necessary to set the property Has Strings of the combobox to True.

like image 63
Martin Avatar answered Oct 28 '25 06:10

Martin