我遇到过这样的问题。我有一个允许通过 UDP 进程间通信的库。这是非常直截了当的。该库创建可供其他进程写入和读取的共享内存。当进程想要读取感兴趣的内存时,它会传递一个字符串值,该字符串值唯一地指向相应的共享内存,并将指针传递给他希望接收读取结果的容器(字符数组)。库提供安全的多线程。
当线程离开 run() 例程时,我有一个异常。
例外:是访问冲突,它在
void __cdecl _freeptd (
_ptiddata ptd
)
{
/*
* Do nothing unless per-thread data has been allocated for this module!
*/
if ( __flsindex != 0xFFFFFFFF ) {
/*
* if parameter "ptd" is NULL, get the per-thread data pointer
* Must NOT call _getptd because it will allocate one if none exists!
* If FLS_GETVALUE is NULL then ptd could not have been set
*/
if ( ptd == NULL
#ifndef _M_AMD64
&& (FLS_GETVALUE != NULL)
#endif /* _M_AMD64 */
)
ptd = FLS_GETVALUE(__flsindex);
/*
* Zero out the one pointer to the per-thread data block
*/
FLS_SETVALUE(__flsindex, (LPVOID)0);
_freefls(ptd);
}
if ( __getvalueindex != 0xFFFFFFFF ) {
/*
* Zero out the FlsGetValue pointer
*/
TlsSetValue(__getvalueindex, (LPVOID)0);
}
}
代码:
char* memory = new char(2000);
string struct_name = "struct";
bool m_running = false;
void Reader::run()
{
initalizeLibrary();
m_running = true;
//loop
while(true)
{
if ( ! m_running ) break;
library->readFromSharedMemory(struct_name, memory);
}
finalize();
}
void Reader::stop()
{
m_running = false;
}
只有当我们允许时才会引发异常library->readFromSharedMemory(struct_name, memory);
。_freeptd
无法访问导致访问冲突的内存。
我需要一只手。提前谢谢。