C++ CString Format()无法读取内存VS2015

C++ CString Format()无法读取内存VS2015,c++,mfc,visual-studio-2015,c-strings,C++,Mfc,Visual Studio 2015,C Strings,我一直收到消息读取字符串字符时出错无法读取内存。我不知道如何更正它,因为调用CString.Format()函数时似乎一切正常 以下是strnlen.cpp中发生错误的位置: 以下是休息时的当地人: 这是调用strnlen.cpp的函数(最后一行是中断发生的位置): m_edit_message是一个CString变量 最后是发生错误时的调用堆栈: NetChess.exe!common_strnlen_c<unsigned char>(const unsigned char *

我一直收到消息
读取字符串字符时出错
无法读取内存
。我不知道如何更正它,因为调用CString.Format()函数时似乎一切正常

以下是strnlen.cpp中发生错误的位置:

以下是休息时的当地人:

这是调用strnlen.cpp的函数(最后一行是中断发生的位置):

m_edit_message是一个CString变量

最后是发生错误时的调用堆栈:

NetChess.exe!common_strnlen_c<unsigned char>(const unsigned char * const string, const unsigned int maximum_count) Line 36  C++
NetChess.exe!common_strnlen_simd<0,unsigned char>(const unsigned char * const string, const unsigned int maximum_count) Line 94 C++
NetChess.exe!common_strnlen<unsigned char>(const unsigned char * const string, const unsigned int maximum_count) Line 153   C++
NetChess.exe!strnlen(const char * string, unsigned int maximum_count) Line 165  C++
NetChess.exe!__crt_stdio_output::output_processor<char,__crt_stdio_output::string_output_adapter<char>,__crt_stdio_output::standard_base<char,__crt_stdio_output::string_output_adapter<char> > >::type_case_s_compute_narrow_string_length(const int maximum_length, char __formal) Line 2268  C++
NetChess.exe!__crt_stdio_output::output_processor<char,__crt_stdio_output::string_output_adapter<char>,__crt_stdio_output::standard_base<char,__crt_stdio_output::string_output_adapter<char> > >::type_case_s() Line 2255  C++
NetChess.exe!__crt_stdio_output::output_processor<char,__crt_stdio_output::string_output_adapter<char>,__crt_stdio_output::standard_base<char,__crt_stdio_output::string_output_adapter<char> > >::state_case_type() Line 1999  C++
NetChess.exe!__crt_stdio_output::output_processor<char,__crt_stdio_output::string_output_adapter<char>,__crt_stdio_output::standard_base<char,__crt_stdio_output::string_output_adapter<char> > >::process() Line 1644  C++
NetChess.exe!common_vsprintf<__crt_stdio_output::standard_base,char>(const unsigned __int64 options, char * const buffer, const unsigned int buffer_count, const char * const format, __crt_locale_pointers * const locale, char * const arglist) Line 163  C++
NetChess.exe!__stdio_common_vsprintf(unsigned __int64 options, char * buffer, unsigned int buffer_count, const char * format, __crt_locale_pointers * locale, char * arglist) Line 235  C++
NetChess.exe!_vscprintf_l(const char * const _Format, __crt_locale_pointers * const _Locale, char * _ArgList) Line 1655 C++
NetChess.exe!_vscprintf(const char * const _Format, char * _ArgList) Line 1672  C++
[External Code] 
NetChess.exe!CMessageSend::OnOK() Line 60   C++
NetChess.exe!_AfxDispatchCmdMsg(CCmdTarget * pTarget, unsigned int nID, int nCode, void(CCmdTarget::*)() pfn, void * pExtra, unsigned int nSig, AFX_CMDHANDLERINFO * pHandlerInfo) Line 77  C++
NetChess.exe!CCmdTarget::OnCmdMsg(unsigned int nID, int nCode, void * pExtra, AFX_CMDHANDLERINFO * pHandlerInfo) Line 372   C++
NetChess.exe!CDialog::OnCmdMsg(unsigned int nID, int nCode, void * pExtra, AFX_CMDHANDLERINFO * pHandlerInfo) Line 85   C++
NetChess.exe!CWnd::OnCommand(unsigned int wParam, long lParam) Line 2779    C++
NetChess.exe!CWnd::OnWndMsg(unsigned int message, unsigned int wParam, long lParam, long * pResult) Line 2092   C++
NetChess.exe!CWnd::WindowProc(unsigned int message, unsigned int wParam, long lParam) Line 2078 C++
NetChess.exe!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 265   C++
NetChess.exe!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 418    C++
[External Code] 
NetChess.exe!CWnd::IsDialogMessageA(tagMSG * lpMsg) Line 193    C++
NetChess.exe!CWnd::PreTranslateInput(tagMSG * lpMsg) Line 4586  C++
NetChess.exe!CDialog::PreTranslateMessage(tagMSG * pMsg) Line 80    C++
NetChess.exe!CWnd::WalkPreTranslateTree(HWND__ * hWndStop, tagMSG * pMsg) Line 3358 C++
NetChess.exe!AfxInternalPreTranslateMessage(tagMSG * pMsg) Line 233 C++
NetChess.exe!CWinThread::PreTranslateMessage(tagMSG * pMsg) Line 777    C++
NetChess.exe!AfxPreTranslateMessage(tagMSG * pMsg) Line 252 C++
NetChess.exe!AfxInternalPumpMessage() Line 178  C++
NetChess.exe!CWinThread::PumpMessage() Line 900 C++
NetChess.exe!CWinThread::Run() Line 629 C++
NetChess.exe!CWinApp::Run() Line 787    C++
NetChess.exe!AfxWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 47   C++
NetChess.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 26  C++
[External Code]

非常感谢您在这方面的任何帮助

代码中有一个问题,此代码:

bool BoundsCheck2(char *myTestArray, unsigned int expectedSize)
// ...
for (size_t i = 0; i < sizeof(myTestArray) / sizeof(myTestArray[0]); i++)     {
    count++;
}
我将改写为:

CString textmsg;
textmsg.Format("%s %s: %s",TEXT.GetString(), comp, m_edit_message.GetString());
假设文本是CString类型-我想它绑定到某个小部件

此外:

作为:

为什么,请阅读这里:


最后,请始终检查Win API函数的结果代码,否则您可能会在未确定状态下使用返回的数据。

什么是
TEXT
?它是如何初始化的?什么是
m_编辑_消息
?它是如何初始化的?调用
gethostname
是否成功?我猜
TEXT
m_edit_message
不是简单的旧C字符串指针(或数组),而是类似于
CString
对象的东西,如果不显式转换为C样式字符串指针,就无法真正使用它。还有什么是
it
?请尝试创建可再现的错误,我不知道它是什么,这是VS正在创建/使用的变量。显示崩溃发生位置的问题是它在标准库代码内,并且代码正常工作。问题在于您的代码中,库中的代码和变量不相关。使用此函数,我试图确保传递给
myTestArray
的变量不超过数组大小
expectedSize
。因此,无法确保
bufferoverlow
occur@Chris-此函数始终返回true(8<50),我使用的是UNICODE,今晚我将实现上面的建议。还将检查更改为
(大小i=0;i
#if !defined(AFX_MESSAGESEND_H__08C3FB4D_9E1E_4AF9_951F_7ED1033E3B16__INCLUDED_)
#define AFX_MESSAGESEND_H__08C3FB4D_9E1E_4AF9_951F_7ED1033E3B16__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MessageSend.h : header file
//

///////////////////////////////////////////CMessageSend dialog

class CMessageSend : public CDialog
{
// Construction
public:
    CMessageSend(CWnd* pParent = NULL);
    void SetReceiveData(char* data);
    // standard constructor
// Dialog Data
    //{{AFX_DATA(CMessageSend)
    enum { IDD = IDD_DIALOG_MESSAGE };
    CString m_edit_message;
    CString m_edit_receive_message;
    //}}AFX_DATA

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
    virtual void OnOK();
    afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

    DECLARE_MESSAGE_MAP()
};

#endif //     !defined(AFX_MESSAGESEND_H__08C3FB4D_9E1E_4AF9_951F_7ED1033E3B16__INCLUDED_)
bool BoundsCheck2(char *myTestArray, unsigned int expectedSize)
// ...
for (size_t i = 0; i < sizeof(myTestArray) / sizeof(myTestArray[0]); i++)     {
    count++;
}
CString textmsg;
textmsg.Format("%s %s: %s",TEXT,comp, (CString) m_edit_message);
CString textmsg;
textmsg.Format("%s %s: %s",TEXT.GetString(), comp, m_edit_message.GetString());
char comp[50];
char comp[256] = {0};