ARC/ObjC++: C++ object inside an ObjC container

2019-07-07 10:33发布

问题:

Consider:

class SomeCppClass {
public:
    SomeCppClass() {} ;
    ~SomeCppClass() {} ;
} ;

@interface Test1 : NSObject

- (id) init ;

@property (strong, nonatomic) NSMutableArray * container ;

@end

@implementation Test1

@synthesize container ;

- (id) init {
    if (self = [super init]) {
        container = [NSMutableArray arrayWithCapacity:10] ;
        [container addObject:[NSValue valueWithPointer:new SomeCppClass()]] ;
    }
    return self ;
}

- (void) dealloc {
    for (NSValue * v in container) {
        SomeCppClass * c = (SomeCppClass *) [v pointerValue] ;
        delete c ;
    }
}
@end

Is this the correct approach to delete C++ land objects when you're done with them under ARC?

回答1:

This will work, but you may consider a couple of other approaches to avoid the NSValue:

  • Create an ObjC wrapper that manages a single instance of SomeCppClass (and deletes just that one object in its dealloc). This can make them a bit easier to deal with in many cases (automatically converting std::string to NSString in the accessors, etc.) This is basically what NSValue is doing for you, but you get much more flexibility by creating your own custom class. This is usually my preferred approach.

  • Store the C++ objects in a C++ container such as vector and then you just have to delete the vector and it's easier to pull things out. You can used shared_ptr to put non-copyable objects into a vector. It is understandable if you don't want the overhead of STL and shared_ptr, but they are readily available in Cocoa.