0

I wanna get a string and put it in EditControl. The Debug Assertion Failed error occurs from the initializing part(SetWindowTextA), but the cause is unknown. m_ChatWindow declared CEdit.

Here is the part of the code:

CMulSenderUIDlg::CMulSenderUIDlg(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_MULSENDERUI_DIALOG, pParent)
{
    m_ChatWindow.SetWindowTextA(" ");
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMulSenderUIDlg::DoDataExchange(CDataExchange* pDX)
{
    .......
    DDX_Control(pDX, IDC_CHATWINDOW, m_ChatWindow);
    .......
}

void CMulSenderUIDlg::RecvData(char* buf, int size)
{
    char* tempBuffer = new char[BUFFSIZE];
    CString currentText;
    m_ChatWindow.GetWindowTextA(currentText);
    currentText += "\n";

    memset(tempBuffer, 0, BUFFSIZE);

    memcpy(tempBuffer, currentText.GetBuffer(), currentText.GetLength());
    memcpy(tempBuffer + currentText.GetLength(), buf, size);

    m_ChatWindow.SetWindowTextA(tempBuffer);
    m_ChatWindow.SetWindowTextA("\r\n");

    delete[] tempBuffer;
}
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 3
    You are doing it too early. In the constructor, no controls are created yet; member variables are not tied to any windows. Override `OnInitDialog`, do your initialization there, after calling the base class' version. – Igor Tandetnik May 25 '20 at 02:42
  • Thank you, but I'll ask you one more question. There is no problem in SetWindowTextA as it is initialized in OnInitDialog, but the GetWindowTextA in the RecvData function has a Debug Assertion failed error. – MoonWhales May 25 '20 at 04:16
  • @MoonWhales what is the EXACT assertion error message? Please be more specific. – Remy Lebeau May 25 '20 at 04:25
  • Just a 'Debug Assertion Failed!' message from Runtime Library. An error occurred in wincore.cpp. And it triggered the breakpoint in GetWindowTextA. – MoonWhales May 25 '20 at 04:32
  • Of course if you get data before the dialog is initialized, you'll get the same issue as doing it in the constructor. Put if(m_ChatWindow.GetSafeWindowHandle()==nullptr){return;} at the start of your CMulSenderUIDlg::RecvData() function. Or buffer the content in a member variable and update the control's text based on that, which I think would be the better design anyway. – Roel May 25 '20 at 04:42
  • I wasn't originally going to use this method. Strangely, I declared a String variable, inserted a buffer, and did UpdateData(), but for the same reason, UpdateData() failed. So I try to change it to this method, but the same error keeps occurring, so I don't know why. I'm sorry I don't seem to be able to explain properly. – MoonWhales May 25 '20 at 04:54
  • @MoonWhales don't describe your code but [edit] the question and show the code _there_. Also check if `RecvData` is called _before_ `OnInitDialog` is called. – Jabberwocky May 25 '20 at 06:15
  • 2
    *"Just a 'Debug Assertion Failed!' message"* - There's a dialog, that comes with the message. And the dialog reads something like *"Press (Retry) to debug the application"*. When you do, you get the expression that failed to evaluate to `true`. Plus the call stack, that lead to the error. With that information available, you can stop guessing and start reasoning. – IInspectable May 25 '20 at 15:58
  • In this era, it is wrong to use `SetWindowTextA()`. You should use `SetWindowText()`, which automatically decides between `SetWindowTextA()` [ASCII] and `SetWindowTextW()` [Unicode]. and your array should be `TCHAR`which automatically decides between `char` [ASCII] and `wchar` [Unicode]. – sergiol May 29 '20 at 21:58

0 Answers0