QThread和读内存
我就遇到过这样的问题。 我有一个允许通过 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
无法访问内存,这会导致访问冲突。
我需要一只手。提前谢谢。
I have faced such a problem.
I have a library which allows through UDP inter-process communication.
It is very straight forward. The library creates shared memory available for other processes to write and read from. When process wants to read a interested memory it passes a string value which uniquely points to corresponding shared memory and passes pointer to container (char array) where he expects to receive result of reading. Library supplies safe multi-threading.
I have a exception when thread has left a run() routine.
Exception: is access violation and it is raised in
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);
}
}
code:
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;
}
Exception is raised only when we allow to library->readFromSharedMemory(struct_name, memory);
. _freeptd
cannot access memory which cause access violation.
I need a hand. Thx in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于此
代码行意味着您只想分配一个字符大小的内存,并且内存由 2000 初始化。
如果您打算分配 2000 个字符大小的内存,您应该使用此代码
并稍后删除它使用
我希望这将帮助您解决访问冲突问题。
I think the problem is the line
this code means you want to allocate only one char size of memory, and the memory is initialized by 2000.
If you are intent to allocate 2000 characters size of memory, you should use this
And delete it later by using
I hope this will help you to fix the access violation issue.
我找到了一个解决方案:
如果分配内存:
char* memory = new char(2000);
它将失败,如果您使用 char* memory = (char*) malloc(2000); 然后相应地释放,它将起作用。我想它有一些新的和不同的编译器分配内存的方式。
卢卡斯。
I have found a solution:
if you allocate memory:
char* memory = new char(2000);
it will fail, if you will use
char* memory = (char*) malloc(2000);
and then free up accordingly, it will work. I suppose it has something with new and different way of allocation a memory by compiler.Lukasz.