Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading AfxBeginThread()+;Cstring=垃圾内容_Multithreading_Mfc_Cstring - Fatal编程技术网

Multithreading AfxBeginThread()+;Cstring=垃圾内容

Multithreading AfxBeginThread()+;Cstring=垃圾内容,multithreading,mfc,cstring,Multithreading,Mfc,Cstring,请帮助我了解我的代码有什么问题 头文件 typedef void (*pStatusCallback)(UINT code, const CString& message); class CComunicator { private: CUT_WSClient _client; bool _shouldTerminate; CString _serverAddress; UINT _serverPort; pStatusCallback _sta

请帮助我了解我的代码有什么问题

头文件

typedef void (*pStatusCallback)(UINT code, const CString& message);

class CComunicator
{
private:
    CUT_WSClient _client;
    bool _shouldTerminate;
    CString _serverAddress;
    UINT _serverPort;
    pStatusCallback _statusCallback;

    UINT _ThreadFunc();
    static UINT ThreadFunc(LPVOID pParam);
public:
    CComunicator(const CString& serverAddress, UINT serverPort, pStatusCallback statusCallback);
    ~CComunicator(void);

    void Start();
    void Stop();
}
CComunicator::CComunicator(const CString& serverAddress, UINT serverPort, pStatusCallback statusCallback) 
{
    _serverAddress = serverAddress;
    _serverPort = serverPort;
    _statusCallback = statusCallback;
}

CComunicator::~CComunicator(void)
{
}

void CComunicator::Start()
{
    _shouldTerminate = false;
    AfxBeginThread(CComunicator::ThreadFunc, this);
}

void CComunicator::Stop()
{
    _shouldTerminate = true;
}

UINT CComunicator::ThreadFunc(LPVOID pParam)
{
    return ((CComunicator*)pParam)->_ThreadFunc(); 
}

UINT CComunicator::_ThreadFunc()
{
    _statusCallback(0, _T("Connecting..."));
    _client.Connect(_serverPort, _serverAddress);
    _statusCallback(0, _T("Connected"));

    // do here some work


    _client.CloseConnection();

    return 0;
}
CComunicator com(_T("10.1.1.105"), 4502, ComunicatorCallback);
com.Start();
源文件

typedef void (*pStatusCallback)(UINT code, const CString& message);

class CComunicator
{
private:
    CUT_WSClient _client;
    bool _shouldTerminate;
    CString _serverAddress;
    UINT _serverPort;
    pStatusCallback _statusCallback;

    UINT _ThreadFunc();
    static UINT ThreadFunc(LPVOID pParam);
public:
    CComunicator(const CString& serverAddress, UINT serverPort, pStatusCallback statusCallback);
    ~CComunicator(void);

    void Start();
    void Stop();
}
CComunicator::CComunicator(const CString& serverAddress, UINT serverPort, pStatusCallback statusCallback) 
{
    _serverAddress = serverAddress;
    _serverPort = serverPort;
    _statusCallback = statusCallback;
}

CComunicator::~CComunicator(void)
{
}

void CComunicator::Start()
{
    _shouldTerminate = false;
    AfxBeginThread(CComunicator::ThreadFunc, this);
}

void CComunicator::Stop()
{
    _shouldTerminate = true;
}

UINT CComunicator::ThreadFunc(LPVOID pParam)
{
    return ((CComunicator*)pParam)->_ThreadFunc(); 
}

UINT CComunicator::_ThreadFunc()
{
    _statusCallback(0, _T("Connecting..."));
    _client.Connect(_serverPort, _serverAddress);
    _statusCallback(0, _T("Connected"));

    // do here some work


    _client.CloseConnection();

    return 0;
}
CComunicator com(_T("10.1.1.105"), 4502, ComunicatorCallback);
com.Start();
用法

typedef void (*pStatusCallback)(UINT code, const CString& message);

class CComunicator
{
private:
    CUT_WSClient _client;
    bool _shouldTerminate;
    CString _serverAddress;
    UINT _serverPort;
    pStatusCallback _statusCallback;

    UINT _ThreadFunc();
    static UINT ThreadFunc(LPVOID pParam);
public:
    CComunicator(const CString& serverAddress, UINT serverPort, pStatusCallback statusCallback);
    ~CComunicator(void);

    void Start();
    void Stop();
}
CComunicator::CComunicator(const CString& serverAddress, UINT serverPort, pStatusCallback statusCallback) 
{
    _serverAddress = serverAddress;
    _serverPort = serverPort;
    _statusCallback = statusCallback;
}

CComunicator::~CComunicator(void)
{
}

void CComunicator::Start()
{
    _shouldTerminate = false;
    AfxBeginThread(CComunicator::ThreadFunc, this);
}

void CComunicator::Stop()
{
    _shouldTerminate = true;
}

UINT CComunicator::ThreadFunc(LPVOID pParam)
{
    return ((CComunicator*)pParam)->_ThreadFunc(); 
}

UINT CComunicator::_ThreadFunc()
{
    _statusCallback(0, _T("Connecting..."));
    _client.Connect(_serverPort, _serverAddress);
    _statusCallback(0, _T("Connected"));

    // do here some work


    _client.CloseConnection();

    return 0;
}
CComunicator com(_T("10.1.1.105"), 4502, ComunicatorCallback);
com.Start();
为什么在方法_ThreadFunc中_serverAddress包含垃圾符号_serverPort的值是否正确? 没有其他人在更改服务器地址


谢谢你的建议。

错误就在你没有粘贴的代码中,就在
com.Start()之后。例如,如果该函数返回,
com
超出范围,字符串也将超出范围。相反,请执行以下操作:

CComunicator *com=NEW CComunicator(_T("10.1.1.105"), 4502, CommunicatorCallback);

com->Start();
这将导致内存泄漏,因此需要
删除com完成后。最好的方法可能是这样:

UINT CComunicator::ThreadFunc(LPVOID pParam)
{
    UNIT ret = ((CComunicator*)pParam)->_ThreadFunc(); 
    delete (CComunicator *)pParam;
    return ret;
}

其他可能性包括将对象保持在作用域中,直到您确定线程已终止(如果创建对象的线程可以保持在相同的作用域中,直到对象的线程完成),以及引用计数对象,其中对象的线程保留对对象的引用。

OMG,David您是对的,在这之后,主方法()的结尾就结束了,程序就完成了。谢谢!