我正在从 uEye 工业相机中提取数据,并通过相机的 API 检索图像。
我的代码看起来像这样:
bool get_image(char*& img)
{
void *pMemVoid; //pointer to where the image is stored
// Takes an image from the camera. If successful, returns true, otherwise
// returns false
if (is_GetImageMem(hCam, &pMemVoid) == IS_SUCCESS){
img = (char*) pMemVoid;
pMemVoid = NULL;
return true;
}
else
return false;
}
我正在获取一张图片数据,如果成功则返回true,否则返回false。
问题是我相信我在用 img = (char*) pMemVoid 泄漏内存,因为我反复调用这个函数而不释放这个数据。如何释放分配给 img 的内存?
编辑:
我在使用 is_AllocImageMem 的函数中初始化相机:
// Global variables for camera functions
HIDS hCam = 0;
char* ppcImgMem;
int pid;
/* Initializes the uEye camera. If camera initialization is successful, it
* returns true, otherwise returns false */
bool init_camera()
{
int nRet = is_InitCamera (&hCam, NULL);
is_AllocImageMem(hCam,752, 480, 1 ,&ppcImgMem, &pid);
is_SetImageMem(hCam, ppcImgMem, pid);
is_SetDisplayMode (hCam, IS_SET_DM_DIB);
is_SetColorMode (hCam, IS_CM_MONO8);
int pnCol , pnColMode;
is_GetColorDepth(hCam, &pnCol , &pnColMode);
is_CaptureVideo(hCam, IS_WAIT);
if (nRet != IS_SUCCESS)
{
if (nRet == IS_STARTER_FW_UPLOAD_NEEDED)
{
hCam = hCam | IS_ALLOW_STARTER_FW_UPLOAD;
nRet = is_InitCamera (&hCam, NULL);
}
cout << "camera failed to initialize " << endl;
return false;
}
else
return true;
}
最佳答案
The API Documentation似乎暗示有一个相应的 is_FreeImageMem 函数。你试过吗?
编辑:看起来 is_GetImageMem 可能没有分配内存。从它的描述:
is_GetImageMem()
returns the starting address of the image memory last used for image capturing.
你在任何地方调用 is_AllocImageMem
吗?
关于c++ - 从 C++ 中的 char* 中释放数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20973737/