我正在开发 C++ 和 Objective-C++ 应用程序。我有 C++ 类,它调用 Objective-c++ 类。在 c++ 标头中,我正在创建 void* ,如下所示:
void *m_self;
然后在构造函数中我像这样实例化objective-c++:
m_self = [[InfoForMac alloc] init];
在析构函数中我这样做:
[(__bridge id)m_self release];
Objective-C++ 类有这个接口:
@interface InfoForMac : NSObject
@property (nonatomic, copy) NSString* data1;
@property (nonatomic, copy) NSString* data2;
@property (nonatomic, assign) int val;
它的初始化方法:
- (id) init
{
if ( (self = [super init]) ) {
self.data1 = @"";
self.data2 = @"";
self.val = -1;
}
return self;
}
及其析构函数:
-(void)dealloc
{
[self.data1 release];
[self.data2 release];
[super dealloc];
}
我从 C++ 调用 Objective-C++ 方法,如下所示:
[(__bridge id)m_self getData1];
QString dataFromMac = QString::from NSString([(__bridge id)m_self data1]);
相关方法如下:
- (void) getData1
{
NSRunningApplication* activeApp = nil;
activeApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
if (activeApp) {
NSString* activeAppLocalizedName = [activeApp localizedName];
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
if (windowList) {
NSArray *windows = (__bridge NSArray*)(windowList);
for (NSDictionary *window in windows) {
NSString* owner = [window objectForKey:@"kCGWindowOwnerName"];
if (activeAppLocalizedName != nil &&
[owner isEqualToString:activeAppLocalizedName]) {
self.data1 = activeAppLocalizedName;
}
}
CFRelease(windowList);
}
}
}
C++ 类被销毁并重新创建。问题是,在我重新创建类并调用 getData1 方法时销毁类后,我在这里遇到了崩溃:
self.data1 = activeAppLocalizedName;
使用调试器堆栈跟踪检查显示该调用,然后显示 objc_msgSend。我已阅读,似乎崩溃可能是由于额外的释放或损坏。
有人可以帮我理解发生了什么吗?
提前致谢和问候