QThread和读内存

发布于 2024-09-13 10:38:18 字数 1810 浏览 1 评论 0原文

我就遇到过这样的问题。 我有一个允许通过 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 技术交流群。

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

发布评论

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

评论(2

心的憧憬 2024-09-20 10:38:18

我认为问题在于此

char* memory = new char(2000);

代码行意味着您只想分配一个字符大小的内存,并且内存由 2000 初始化。

如果您打算分配 2000 个字符大小的内存,您应该使用此代码

char* memory = new char[2000];

并稍后删除它使用

delete [] memory;

我希望这将帮助您解决访问冲突问题。

I think the problem is the line

char* memory = new char(2000);

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

char* memory = new char[2000];

And delete it later by using

delete [] memory;

I hope this will help you to fix the access violation issue.

我不吻晚风 2024-09-20 10:38:18

我找到了一个解决方案:

如果分配内存: 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.

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