在向MFC组合框添加新字符串时触发了断言

时间:2014-02-24 15:16:53

标签: c++ mfc

我正在测试一个旧的MFC应用程序,当在调试模式下运行时,断言它总是在尝试向组合框添加新元素时触发。

我对MFC了解不多,但我看到在添加元素之前,DoDataExchange已调用它,也许这里的组合框已经初始化了?

DDX_Control(pDX, IDC_BAUDRATE, m_comboBaudRate);

但是,在执行此操作时,在OnInitDialog中:

m_comboBaudRate.AddString((CString)port[0]);

触发此断言:

_AFXWIN_INLINE int CComboBox::AddString(LPCTSTR lpszString)
{ 
   ASSERT(::IsWindow(m_hWnd));
   .........................
}

任何人都知道为什么会这样,我的解决方案是什么?

编辑:添加了OnInitDialog代码:

CDialogMgr::OnInitDialog();
    //set the window text from message table
    SetWindowText(theApp.GetAtlasCaptionOrMsg(IDD_PCCONFIGURATION_CAPTION));

    ///Adding data from message table
    m_btnOk.SetWindowText(theApp.GetAtlasCaptionOrMsg(IDD_BTN_OK));
    m_btnHelp.SetWindowText(theApp.GetAtlasCaptionOrMsg(IDD_BTN_HELP));
    m_btnCancel.SetWindowText(theApp.GetAtlasCaptionOrMsg(IDD_BTN_CANCEL));

    m_staticDeviceId.SetWindowText(theApp.GetAtlasCaptionOrMsg(IDD_PCCONFIGURATION_DEVICEID));
    m_staticComPort.SetWindowText(theApp.GetAtlasCaptionOrMsg(IDD_PCCONFIGURATION_COMPORT));
    m_staticBaudRate.SetWindowText(theApp.GetAtlasCaptionOrMsg(IDD_PCCONFIGURATION_BAUDRATE));


    //For com port
    TCHAR   PortName[MAX_PORT_NO+1][MAX_COMPORT_STR_LEN]={0};
    int portCount=0;
    int NoPort=searchComPort(PortName);
    if(NoPort==0)
    {
        AfxMessageBox(theApp.GetAtlasCaptionOrMsg(IDD_MSG_COMPORTNOTFOUND));
        EndDialog(IDCANCEL);
        return FALSE;
    }
    else
    {
        //For adding the com port in the combo box
        while(PortName[portCount][0]!=0)
        {
            m_comboComPort.AddString(PortName[portCount]);
            portCount++;
        }
    }

    //For Adding baud rate
    int iBaudRateInd=0;
    char BaudRate[80];
    CComBSTR ccmbstrBaudRate[10];

    theApp.GetProductObject()->getBaudRate(&ccmbstrBaudRate[0]);
    wcstombs(BaudRate,ccmbstrBaudRate[0],80);
    char port[5][6];
    int j;
    int i;
    for( i=0;i<5;i++)
        for(j=0;j<6;j++)
            port[i][j]='\0';
    i=0;
    j=0;

    while(BaudRate[i]!=',')
    {
        port[0][j]=BaudRate[i];
        i++;
        j++; 
    }
    i++;
    j=0;
    iBaudRateInd++;
    m_comboBaudRate.AddString((CString)port[0]);

    while(BaudRate[i]!=',')
    {
        port[1][j]=BaudRate[i];
        i++;
        j++;
    }
    i++;

    iBaudRateInd++;
    m_comboBaudRate.AddString((CString)port[1]);
    j=0;
    while(BaudRate[i]!=',')
    {
        port[2][j]=BaudRate[i];
        i++;
        j++;
    }
    i++;

    iBaudRateInd++;
    m_comboBaudRate.AddString((CString)port[2]);
    j=0;        
    while(BaudRate[i]!=',') 
    {
        port[3][j]=BaudRate[i];
        i++;
        j++;

        if(BaudRate[i]=='\0')
            break;
    }

    iBaudRateInd++;
    m_comboBaudRate.AddString((CString)port[3]);

    if(BaudRate[i]!='\0')
    {
        i++;
        j=0;

        while(BaudRate[i]!=',')
        {
            port[4][j]=BaudRate[i];
            i++;
            j++;
            if(BaudRate[i]=='\0')
                break;

        }

        iBaudRateInd++;
        m_comboBaudRate.AddString((CString)port[4]);

    }

    GetPrivateProfileString("RS232_2","BaudRate","9600",BaudRate,sizeof(BaudRate),CONFIG_GENERIC_INI);

    char  ComPort[10];
    GetPrivateProfileString("RS232_2","COMPort","COM1",ComPort,sizeof(ComPort),CONFIG_GENERIC_INI);

    int indexBaudRate=m_comboBaudRate.FindString(-1,(CString)BaudRate);

    m_comboBaudRate.SetCurSel(indexBaudRate);

    int indexComPort=m_comboComPort.FindString(-1,(CString)ComPort);
    m_comboComPort.SetCurSel(indexComPort);

    //Getting the default value
    char  DeviceID[10];
    GetPrivateProfileString("Secs1_1","DeviceId","251",DeviceID,sizeof(DeviceID),CONFIG_GENERIC_INI);
    GetPrivateProfileString("SecsII_1","DeviceId","251",DeviceID,sizeof(DeviceID),CONFIG_GENERIC_INI);
    m_lDeviceId=atoi(DeviceID);

    UpdateData(false);

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

1 个答案:

答案 0 :(得分:2)

您不要从OnInitDialog中的父类调用OnInitDialog。添加CDialog :: OnInitDialog()作为OnInitDialog的第一行。 (我假设你的对话继承自CDialog)。