栈上的c++类实例可以被Objective-C块捕获吗?
Can C++ class instances on the stack be captured by Objective-C blocks?
当试图在Objective-C块中捕获堆栈上的c++类实例时,我看到了一些奇怪的行为。考虑下面的代码:
#import <Foundation/Foundation.h>
#include <stdio.h>
struct Test
{
Test() : flag(0) { printf("%p default constructorn", this); }
Test(const Test& other) : flag(0) { printf("%p copy constructorn", this); }
~Test() { flag = 1; printf("%p destructorn", this); }
int flag;
};
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Test test;
void (^blk)(void) = ^(void)
{
printf("flag=%d (test=%p)n", test.flag, &test);
};
printf("about to call blkn");
blk();
[pool release];
return 0;
}
可以预期,当局部变量test
被块blk
捕获时,它具有一致的状态。然而,事实并非如此。这段代码的输出如下:
0x7fff5fbff650 default constructor
0x7fff5fbff630 copy constructor
about to call blk
0x7fff5fbff5d0 copy constructor
0x7fff5fbff5d0 destructor
flag=1 (test=0x7fff5fbff5d0)
0x7fff5fbff630 destructor
0x7fff5fbff650 destructor
所以块看到的本地Test
实例已经调用了析构函数!你用它做的任何事情都是未定义的行为,这很可能导致崩溃(例如,如果析构函数删除了一个指针而没有将其设置为NULL)。
c++类实例变量被Objective-C块支持吗?块编程主题部分"c++对象"似乎表明它们是,但它们显然在这里不起作用。这是编译器/运行时的错误吗?这是在GCC 4.2.1, Apple build 5666, Mac OS X v10.6.8上测试的。
我认为这是GCC中的一个bug。
当我在GCC中尝试时,我得到:
0x7fff5fbff5d0 default constructor
0x7fff5fbff5c0 copy constructor
about to call blk
0x7fff5fbff570 copy constructor
0x7fff5fbff570 destructor
flag=1 (test=0x7fff5fbff570)
0x7fff5fbff5c0 destructor
0x7fff5fbff5d0 destructor
但是使用LLVM 2.0:
0x7fff5fbff610 default constructor
0x7fff5fbff600 copy constructor
about to call blk
flag=0 (test=0x7fff5fbff600)
0x7fff5fbff600 destructor
0x7fff5fbff610 destructor
后者遵循我对块文档的解释(并且是唯一没有明显破坏的版本)。
相关文章:
- 如何将数组从Objective-C发送到C++函数
- 在 Objective-C++ -> EXC_BAD_ACCESS Error 中使用"new"关键字
- Mac Snow Leopard 上的 Qt 5.3.2:ld:在架构x86_64 [clang、Objective-C
- 创建跨平台 C++ 触摸管理器.在 c++ 中传递 Objective-c 对象涉及代码
- C++对象作为 Objective-C++ 属性
- 将.cpp文件中的数据传递给Objective c ViewController
- 为什么这个来自 Objective-C++ 的dynamic_cast在调试中成功但在发布时失败?
- Objective-C 如何使用向量对?
- 在 Objective-C 和 Swift 之间委托问题
- Objective-C C++ 包装器 类型错误的不完整定义
- EXC_BAD_ACCESS Xcode,来自我的 Objective-C++ 和 swift 桥接标头中的 C++ 包
- 在 Objective-C++ 中应用于__weak指针时,通过关键字推导类型"auto"规则是什么?
- 如何发送(和接收)从 Swift 到 Objective c++ 再到 c++,然后备份到 Swift 的超大型浮动数组
- 使用来自Objective C的OpenCV分解同源异构图
- 如何将这行代码从C++转换为Objective-C
- Firebase:混合使用C++和Objective-C SDK可以吗
- 如何使用Visual Studio C/C++编译器(cl.exe)来预处理我的objective-C代码
- 有没有办法将函数从 objective-c++ 传递到标准 c++
- 从Objective-C中的C 标头读取变量
- Objective-C 块中的泄漏C++ shared_ptr