gpt4 book ai didi

c++ - 尝试为图像缓冲区分配内存时错误的 ptr 值

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:24 25 4
gpt4 key购买 nike

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 值。

输出:

First-chance exception at 0x0c10e473 in improc.exe: 0xC0000005: Access violation reading location 0x00000000. Unhandled exception at 0x0c10e473 in improc.exe: 0xC0000005: Access violation reading location 0x00000000.

我关注的教程视频:https://youtu.be/j9vb5UjQCQg

最佳答案

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;
}

关于c++ - 尝试为图像缓冲区分配内存时错误的 ptr 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41730512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com