AfxBeginThread() + Cstring = 垃圾内容
请帮助我理解我的代码有什么问题。
头文件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误位于您未粘贴的代码中,就在
com.Start();
之后。例如,如果该函数返回,com
就会超出范围,字符串也会超出范围。相反,请执行以下操作:这将导致内存泄漏,因此您需要在使用完毕后
删除 com;
。最好的方法可能是这样的:其他可能性包括将对象保留在范围内,直到您确定线程已终止(如果创建对象的线程可以保留在同一范围内,直到对象的线程完成),并对对象进行引用计数对象,其中对象的线程保存对该对象的引用。
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: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: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.