Vinay
Vinay

Reputation: 149

Memory management between ARC and non-ARC frameworks

I have a framework A which is ARC. This consumes some API's from non ARC framework B.

Framework B sample code (non ARC):

@interface Settings : NSObject {
    NSDictionary* settings;
}
- (NSString*)stringForKey:(NSString*)key;
@end
@implementation Settings
- (NSString*)stringForKey:(NSString*)key {
    return [settings objectForKey: key];
}
@end

Framework A sample code (ARC):

{
    // ...
    SEL stringForKeySelector = @selector(stringForKey:);
    NSMethodSignature *stringForKeySignature = [[Settings class] instanceMethodSignatureForSelector:stringForKeySelector];
    NSInvocation *stringForKeyInvocation = [NSInvocation invocationWithMethodSignature:stringForKeySignature];
    [stringForKeyInvocation setSelector:stringForKeySelector];
    [stringForKeyInvocation setTarget:SettingsObject];

    NSString *Key = @"KEY";
    [stringForKeyInvocation setArgument:& Key atIndex:2];
    [stringForKeyInvocation invoke];

    NSString *value = nil;
    [stringForKeyInvocation getReturnValue:& value];

    // ...
}

Object from settings dictionary gets released after executing above code in framework A.

Any help is appreciated in advance.

Answer: Thanks Chuck for pointing out problems with NSInvocation and ARC. I got around this problem by returning basic data types.

Upvotes: 1

Views: 204

Answers (1)

Chuck
Chuck

Reputation: 237110

This has nothing to do with memory management between ARC and non-ARC code. Instead, this is because NSInvocation does not work very cleanly with ARC. It's usually preferable to avoid NSInvocation anyway, and doubly so with ARC. There is usually a better choice for anything you'd use NSInvocation for. If you must use NSInvocation, you'll need to remember that it just deals with raw byte blobs and doesn't handle object ownership at all, so passing an ARC-managed pointer is not kosher. You'll want to have the return-value variable be a void pointer, and then use a bridging cast to assign it to a normal object-type variable.

Upvotes: 1

Related Questions