0

所以我遇到了堆问题的损坏,想知道它是否与我如何处理某些数组有关。实际上,在某些时候,我有一些这样的消息发送到我的 WinProc:

SendMessage(hwnd, LOG_ADD, NULL, (LPARAM)L"Initializing winsock... ");

LOG_ADD 定义为 104。在 WinProc 内部,当 msg==LOG_ADD 时,会发生以下情况:

case LOG_ADD:
            {
                pGame->pMessageLog->PutToLog((WCHAR*)lParam);
                pGame->pD2DResources->OnRender(pGame->pMessageLog);

有 MessageLog::PutToLog(WCHAR[]):

void MessageLog::PutToLog(WCHAR txt[])
{
    int strLen=wcslen(txt);
    int logLen=wcslen(logHistory);

    WCHAR* pWCHAR = txt;

    int x=0;

    // Counts the number of '\n' into wString and adds that number to nLogLines
    for(x=0; x<strLen; x++)
    {
        if(pWCHAR[x]=='\n')
            nLogLines++;
    }

    pWCHAR = logHistory;

    x=0;
    while(nLogLines>5)
    {
        if(pWCHAR[x]=='\n')
            nLogLines--;
        x++;
    }

    if(x!=0)
    {
        for(int y=0; y<logLen-x; y++)
        {
            pWCHAR[y]=pWCHAR[y+x];
        }
    }

    wcscat (logHistory, txt);
}

这能解释腐败问题吗?实际上,当我删除所有 SendMessage(hwnd, LOG_ADD...) 行时,当编译器执行“struct hostent* host;”行时,损坏不会在几行后出现。或“如果((主机=gethostbyname(服务器))==NULL)”。

4

1 回答 1

2

You asked in a comment:

is this the proper way of sending a WCHAR array as a LPARAM and sending it to another function to use?

Sure, as long as you know what you pass at the call site and make sure you treat it (cast it) as the type it actually is in the window procedure, this is perfectly fine.

Just make sure you're properly handling any pointers to memory that might be invalid at the time you access it. For instance if you post a message instead of sending it and pass a pointer to memory that runs out of scope before the receiver handles the message.

Also remember to properly initialize any data you send...

于 2013-03-19T01:01:28.033 回答