Objective-C块,c++变量和异步代码
Objective-C Blocks, C++ Variables and async code
在iOS上将c++变量与obj-c异步代码粘合在一起时遇到问题。
真正的问题在async代码中,我使用的是c++中内置的第三方库,需要对象引用,例如:
<>之前
- (void) processFrame:(cv::Mat &)mat;
之前我的问题真正的问题是怎么称呼这个?我需要在不同的线程上创建c++对象,并将其传递给异步代码,有点像这样:
__block cv::Mat mat(videoRect.size.height, videoRect.size.width, CV_8UC4, baseaddress, 0);
dispatch_async(dispatch_get_main_queue(), ^{
[self processFrame:mat];
});
Which give an error (Bad access), the problem is (I guess) the object is destroyed before the method runs, so I tried creating the object in the heap:
__block cv::Mat *mat= new cv::Mat(videoRect.size.height, videoRect.size.width, CV_8UC4, baseaddress, 0);
dispatch_async(dispatch_get_main_queue(), ^{
[self processFrame:(*mat)];
});
And still:
__block cv::Mat *mat= new cv::Mat(videoRect.size.height, videoRect.size.width, CV_8UC4, baseaddress, 0);
dispatch_async(dispatch_get_main_queue(), ^{
[self processFrame:mat];
});
I get keeping "Bad access" errors all the time
Any ideas ?
The __block
qualifier tells the compiler to not copy the object for the block so that is why it is failing. If you are not reassigning mat
or are not trying to prevent an unnecessary retain/copy operation then you should remove __block
.
cv::Mat mat(videoRect.size.height, videoRect.size.width, CV_8UC4, baseaddress, 0);
dispatch_async(dispatch_get_main_queue(), ^{
[self processFrame:mat];
});
创建new cv::Mat
的示例可能会失败,因为您可能过早地删除了mat
。
__block cv::Mat *mat= new cv::Mat(videoRect.size.height, videoRect.size.width, CV_8UC4, baseaddress, 0);
dispatch_async(dispatch_get_main_queue(), ^{
[self processFrame:mat];
//mat would need to be deleted here
});
delete mat; //If you are doing this, you will likely get an EXC_BAD_ACCESS
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 使用 std::chrono::steady_clock 对线程/异步中的代码进行基准测试
- 异步接口托管 - > 非托管代码
- 有没有办法为任何函数编写通用代码,以便它可以(异步)执行并从线程池中获得返回值?
- STD ::承诺外部代码,异步取消
- 将阻止同步代码转换为异步
- 如何使用从Swift代码中调用的线程在C 上制作异步调用功能
- 异步调用槽,而不使用清晰的代码行连接到它
- 如何测试异步代码
- 如何在C++中创建异步/等待代码
- 对ReadFile函数的异步调用返回6个错误代码
- Objective-C块,c++变量和异步代码
- 异步c++代码中的内存管理
- 在V8中异步运行c++和JS代码