【问题标题】:Bad ptr value when trying to allocate the memory for the image buffer尝试为图像缓冲区分配内存时 ptr 值错误
【发布时间】:2017-06-03 11:40:19
【问题描述】:
unsigned char* createImageBuffer(unsigned int bytes)
{
    unsigned char* ptr;
    cudaSetDeviceFlags(cudaDeviceMapHost);
    cudaHostAlloc(&ptr, bytes, cudaHostAllocMapped);    
    return ptr;
}


    cv::Mat sGray(frame.size(), CV_8U, createImageBuffer(frame.size().width * frame.size().height));

    cv::Mat dGray(frame.size(), CV_8U, createImageBuffer(frame.size().width * frame.size().height));

    cv::Mat eGray(frame.size(), CV_8U, createImageBuffer(frame.size().width * frame.size().height));

调试期间的问题是关于访问 0x00000000 处的受限内存。该函数应该返回一个指向图像字节分配位置的指针,但是它给了我错误的 ptr 值。

输出:

improc.exe 中 0x0c10e473 处的第一次机会异常:0xC0000005:访问 违规读取位置 0x00000000。未处理的异常 improc.exe 中的 0x0c10e473:0xC0000005:读取访问冲突 位置 0x00000000。

我跟随的教程的视频:https://youtu.be/j9vb5UjQCQg

【问题讨论】:

  • 你确定frame不为空吗?
  • 访问冲突读取位置 0x00000000 表示 createImageBuffer 返回空指针。最可能的原因是 @ilke444 指出的 - frame.size() 返回 0。

标签: c++ opencv pointers cuda


【解决方案1】:

API 可能无法分配足够的内存。检查 cudaHostAlloc 的返回值(参见doku on cudaHostAlloc):

unsigned char* createImageBuffer(unsigned int bytes)
{
    unsigned char* ptr;
    cudaSetDeviceFlags(cudaDeviceMapHost);
    cudaError_t allocResult = cudaHostAlloc(&ptr, bytes, cudaHostAllocMapped);
    if (allocResult != cudaSuccess) {
      // could be cudaErrorMemoryAllocation; The API call failed because
      // it was unable to allocate enough memory to perform
      // the requested operation.
      return nullptr;
    }
    return ptr;
}

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 2023-03-15
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 2011-09-03
    相关资源
    最近更新 更多