Objective-C 块,C++变量和异步代码

发布于 2024-12-19 23:12:10 字数 1005 浏览 2 评论 0原文

我在 iOS 上将 c++ 变量与 obj-c 异步代码粘合在一起时遇到问题。

真正的问题位于异步代码中,我正在使用 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];
        });

这给出了错误(访问错误),问题是(我猜)对象在方法运行之前被销毁,所以我尝试在堆中创建对象:


__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)];
        });

仍然:


__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'm having an issue while glueing together c++ vars with obj-c async code on iOS.

The real problem is located in the async code, I'm using third-party libraries built in C++ that expect object references, e.g.:


- (void) processFrame:(cv::Mat &)mat;

My problem real problem is how to call this ? I need to create the c++ object on a different thread and pass it to the async code, a bit like this:


__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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2024-12-26 23:12:10

__block 限定符告诉编译器不要复制块的对象,这就是它失败的原因。如果您不重新分配mat或不试图阻止不必要的保留/复制操作,那么您应该删除__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

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

The examples where you create a new cv::Mat may be failing because you may be deleting mat too soon.

__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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文