使用Intel的OpenCL SDK获取cl_platform_id释放free时已触发了一个断点的解决

wangluojisuan 2015-09-22 12:52:27
今天在编写代码的时候发现一个问题,是 使用Intel的OpenCL SDK获取cl_platform_id释放free时已触发了一个断点。自己找到了解决的方案,记录一下。上代码

cl_uint num_platforms;
cl_platform_id *platforms = NULL;

cl_int ret = clGetPlatformIDs(5, NULL, &num_platforms);
if (0 == ret){
printf("已获取平台数量:%d\n", num_platforms);
}
else{
printf("获取平台数量失败,请检查\n");
return 1;
}
platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id)*num_platforms);
ret = clGetPlatformIDs(5, platforms, NULL);

char platform_vendor[40];
ret = clGetPlatformInfo(*platforms, CL_PLATFORM_VENDOR, sizeof(platform_vendor), platform_vendor, NULL);
if (0 == ret){
cout << "平台厂商:"<< platform_vendor << endl;
}
else{
cout << "获取平台信息失败!" << endl;
return 1;
}
free(platforms);
system("pause");

每次运行这段代码都会提示一个, 已触发了一个断点。追踪问题,发现是free()的问题,甚至把代码精简成这样也报错

//平台结构
cl_platform_id *platforms = NULL;
cl_uint num_platform = 0;

int ret = clGetPlatformIDs(3, NULL, &num_platform);
if (ret == 1){
//cout << "函数执行错误,请检查!" << endl;
return 1;
}
//cout << "机器平台数量:" << num_platform << endl;
printf("机器平台数量:%d\n", num_platform);
platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id)*num_platform);
ret = clGetPlatformIDs(3, platforms, NULL);
free(platforms);
system("pause");


最后查来查去发现是两次clGetPlatformIDs的问题,我在两次执行clGetPlatformIDs时,写死了数字,如果改成这样就没有问题了。

//平台结构
cl_platform_id *platforms = NULL;
cl_uint num_platform = 0;

int ret = clGetPlatformIDs(3, NULL, &num_platform);
if (ret == 1){
//cout << "函数执行错误,请检查!" << endl;
return 1;
}
//cout << "机器平台数量:" << num_platform << endl;
printf("机器平台数量:%d\n", num_platform);
platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id)*num_platform);
ret = clGetPlatformIDs(num_platform , platforms, NULL);
free(platforms);
system("pause");

目前还不知道原因,有懂得大侠可以解释下。
...全文
1355 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangluojisuan 2015-09-24
  • 打赏
  • 举报
回复
引用 2 楼 wangluojisuan 的回复:
[quote=引用 1 楼 bluewanderer 的回复:] 申请的内存溢出了当然再用堆API的时候可能会挂。
我想问一下,是如何溢出的?本身我的机器上的OpenCL平台只有1个Intel的。[/quote] 谢谢
bluewanderer 2015-09-23
  • 打赏
  • 举报
回复
比如实现蛋疼地把你的缓冲清零了... 出问题的时候往最不邪门的地方想往往是最可靠的,黑话说就叫奥卡姆剃刀法则
wangluojisuan 2015-09-23
  • 打赏
  • 举报
回复
引用 1 楼 bluewanderer 的回复:
申请的内存溢出了当然再用堆API的时候可能会挂。
我想问一下,是如何溢出的?本身我的机器上的OpenCL平台只有1个Intel的。
bluewanderer 2015-09-22
  • 打赏
  • 举报
回复
申请的内存溢出了当然再用堆API的时候可能会挂。

602

社区成员

发帖
与我相关
我的任务
社区描述
异构开发技术
社区管理员
  • OpenCL和异构编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧