在 C++ dll 中删除指针时出错
Error when deleting pointer in c++ dll
我用 visual c++ 编写了一个 dll,其中包含我从 labview 调用的函数。我在初始化时创建一个指针,将该指针传递出去,然后在调用其他函数时使用它。一切正常,直到我尝试删除该指针,然后我得到一个 "Unhandled exception at 0x56DC514A (intelcamera.dll) in LabVIEW.exe: 0xC000001D: Illegal Instruction." 这是我导出的函数:
extern "C" int colorstream_init(uintptr_t *ptrOut, int *data)
{
*ptrOut = (uintptr_t)new CDevice();
((CDevice*)*ptrOut)->init_camera();
*data = ((CDevice*)*ptrOut)->get_data();
return ((CDevice*)*ptrOut)->get_sts();
}
extern "C" int get_image(uintptr_t ptr, uint32_t image[], int size, int *data)
{
int rtn_val = ((CDevice*)ptr)->get_image(image, size);
*data = ((CDevice*)ptr)->get_data();
return rtn_val;
}
extern "C" int close(uintptr_t ptr, uint32_t last_image[], int size)
{
// ((CDevice*)ptr)->get_image(last_image, size);
int r = ((CDevice*)ptr)->close();
delete (CDevice*)ptr;
return r;
}
我尝试将 "delete (CDevice*)ptr;" 行移动到 get_image 函数中并得到相同的异常。
有人可以帮忙吗?
我发现了问题;感谢所有帮助过的人。我发现在 labview 库调用中,我将 ptr 输入参数作为 "Pointer to Value" 而不是 "Value" 传递。我将它作为 "Pointer to Value," 从 init 函数传递出去,但它需要作为 "Value" 传回(显然...)。
这是我第一次在 .dll 中编写函数,对于一个愚蠢的错误占用了人们的时间,我深表歉意。
我用 visual c++ 编写了一个 dll,其中包含我从 labview 调用的函数。我在初始化时创建一个指针,将该指针传递出去,然后在调用其他函数时使用它。一切正常,直到我尝试删除该指针,然后我得到一个 "Unhandled exception at 0x56DC514A (intelcamera.dll) in LabVIEW.exe: 0xC000001D: Illegal Instruction." 这是我导出的函数:
extern "C" int colorstream_init(uintptr_t *ptrOut, int *data)
{
*ptrOut = (uintptr_t)new CDevice();
((CDevice*)*ptrOut)->init_camera();
*data = ((CDevice*)*ptrOut)->get_data();
return ((CDevice*)*ptrOut)->get_sts();
}
extern "C" int get_image(uintptr_t ptr, uint32_t image[], int size, int *data)
{
int rtn_val = ((CDevice*)ptr)->get_image(image, size);
*data = ((CDevice*)ptr)->get_data();
return rtn_val;
}
extern "C" int close(uintptr_t ptr, uint32_t last_image[], int size)
{
// ((CDevice*)ptr)->get_image(last_image, size);
int r = ((CDevice*)ptr)->close();
delete (CDevice*)ptr;
return r;
}
我尝试将 "delete (CDevice*)ptr;" 行移动到 get_image 函数中并得到相同的异常。
有人可以帮忙吗?
我发现了问题;感谢所有帮助过的人。我发现在 labview 库调用中,我将 ptr 输入参数作为 "Pointer to Value" 而不是 "Value" 传递。我将它作为 "Pointer to Value," 从 init 函数传递出去,但它需要作为 "Value" 传回(显然...)。
这是我第一次在 .dll 中编写函数,对于一个愚蠢的错误占用了人们的时间,我深表歉意。