You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

读取共享内存后触发标准工具头读取访问违例问题求助

读取共享内存后触发标准工具头读取访问违例问题求助

各位大佬好,我遇到一个棘手的问题,想请教下大家。

我们现有一个程序,负责从传感器拉取数据,然后通过共享内存和PLC交互——这部分目前运行完全正常。现在我在同一台PC上开发另一个应用,打算读取这块共享内存来做UI展示、日志输出等功能。

目前我已经能正确读取共享内存拿到当前值了,但奇怪的是,当读取函数执行完成后,会抛出一个来自标准工具头的读取访问违例。

用调试器单步执行的时候,能看到getData()函数确实拿到了正确的值,newData.name和newData.id_number都能正常输出,但当main()函数结束时,就会触发异常:

Exception thrown: read access violation.
_Val was 0x1B093CC83D8.

以下是我的代码:

#include <string>
#include <iostream>
#include <tchar.h>
#include <windows.h>

const int kBufSize = 4096;

struct Test_Data_Struct {
    std::string name;
    int id_number;
};

struct CommunicationParameters {
    HANDLE hMapFile;    //  handler
    LPCTSTR pBuf;   //  buffer
    int status; //  status of shared memory
};

CommunicationParameters CreateOrAccessSharedMemory(LPCTSTR lpName) {
    HANDLE hMapFile = NULL;
    LPCTSTR pBuf;

    hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS,   // read/write access
        FALSE,                 // do not inherit the name
        lpName);               // name of mapping object

    //  if not exist
    if (hMapFile == NULL)
    {
        hMapFile = CreateFileMapping(
            INVALID_HANDLE_VALUE,    // use paging file
            NULL,                    // default security
            PAGE_READWRITE,          // read/write access
            0,                       // maximum object size (high-order DWORD)
            kBufSize,                // maximum object size (low-order DWORD)
            lpName);                 // name of mapping object
    }

    //  check whether shared memory is created successfully
    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).\n"),
            GetLastError());
    }

    pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object
        FILE_MAP_ALL_ACCESS, // read/write permission
        0,
        0,
        kBufSize);

    if (pBuf == NULL)
    {
        _tprintf(TEXT("Could not map view of file (%d).\n"),
            GetLastError());
        CloseHandle(hMapFile);
    }

    CommunicationParameters shared_memory;
    if (pBuf != NULL || hMapFile != NULL) {
        shared_memory.hMapFile = hMapFile;
        shared_memory.pBuf = pBuf;
        shared_memory.status = 1;
    }
    else {
        shared_memory.status = 0;
    }

    return shared_memory;
}

Test_Data_Struct getData()
{
    LPCTSTR test_data_kernel_location = L"Global\\Test_Object_1";
    CommunicationParameters test_data_kernel_object = CreateOrAccessSharedMemory(test_data_kernel_location);

    Test_Data_Struct incoming_data;
    memcpy(&incoming_data, test_data_kernel_object.pBuf, sizeof(incoming_data));

    // Unmap view of file and close file mapping object handles
    UnmapViewOfFile(test_data_kernel_object.pBuf);
    CloseHandle(test_data_kernel_object.hMapFile);

    return incoming_data;
}

int main() {
    Test_Data_Struct newData = getData();
    std::cout << newData.name << " " << newData.id_number;
}

我已经做了一些排查尝试:

  • 如果注释掉getData()里的memcpy函数,只返回一个简单类型(比如整数),就不会触发异常
  • 试过用CopyMemory()替代memcpy,结果完全一样
  • 我还有另一个程序,循环每1秒读取共享内存并打印到控制台,那个程序运行完全正常——看起来问题不是出在读取过程中,而是函数执行完毕之后

有没有大佬能帮我分析下问题出在哪?

备注:内容来源于stack exchange,提问作者Kyle Wagner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 15:09:30