AfxBeginThread() + Cstring = 垃圾内容

发布于 2024-12-12 06:03:26 字数 1643 浏览 0 评论 0原文

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

头文件

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 的值正确吗? 没有其他人改变 _serverAddress。

感谢您的任何想法。

Please help me understand what is wrong with my code.

Header File

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();
}

Source file

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;
}

Usage

CComunicator com(_T("10.1.1.105"), 4502, ComunicatorCallback);
com.Start();

Why in method _ThreadFunc the _serverAddress contains garbage symbols? _serverPort has the correct value?
Nobody else is altering the _serverAddress.

Thx for any ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2024-12-19 06:03:26

该错误位于您未粘贴的代码中,就在 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;
}

其他可能性包括将对象保留在范围内,直到您确定线程已终止(如果创建对象的线程可以保留在同一范围内,直到对象的线程完成),并对对象进行引用计数对象,其中对象的线程保存对该对象的引用。

The bug is in the code you didn't paste, right after com.Start();. For example, if that function returns, com goes out of scope, taking the string out of scope too. Instead, do this:

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

com->Start();

This will cause a memory leak, so you need to delete com; when you're done with it. The best way might be like this:

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

Other possibilities include keeping the object in scope until you're sure the thread has terminated (if the thread that created the object can remain in that same scope until the object's thread is done) and reference counting the object, where the object's thread holds a reference to the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文