0
votes

I have a program that uses ARC and call in some library methods that are non ARC.

Non ARC Library:

-(NSMutableData*) bar{
    return [[NSMutableData alloc] initWithLength:100];
}

ARC Program:

- (void)foo
{
    NSMutableData* data = [d bar];
}
// Data is leaked

Is possible to avoid data being leaked without change the library method to return an autoreleased object?

When i use this library in Non ARC code i used to call release on data and thus avoid the leak.

1
Your non-ARC library is broken. Fix it. You'll be far better off in the long run if you. - bbum
A basic memory management rule says (see developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/…) "You own any object you create ... using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy”". Since this is not the case for method "bar", the ARC program assumes that it is not the owner of the object instantiated in bar, and so it leaks. The only correct way to fix it is to return an autorelease object, as Lithu T.V suggested. - Reinhard Männer
There is also the possibility of mark the method with NS_RETURNS_RETAINED which was what i was looking for. - José

1 Answers

2
votes

How about

-(NSMutableData*) bar
{
    return [[[NSMutableData alloc] initWithLength:100] autorelease];
}