1
votes

Dear iPhone Developers, I have an instance method which is meant to return a string

- (NSString *)newFile:(NSString *)inFile andFileNumber:(NSInteger)aNumber {
    return [NSString stringWithFormat:@"%@.o%i",inFile,aNumber];
}

I call this method as

outputFileName = [self newFile:inputFileName andFileNumber:newNumber];
// where inputFileName is a string and newNumber is an integer 
// outputFileName (also a string), inputFileName and newNumber are declared in
// the interface and in the implementation

When I compile the project with Analyzer, it gives the following messages;

  1. Method returns an Objective-C object with a +0 retain count (non-owning reference)
  2. Object returned to caller with a +0 (non-owning) retain count
  3. Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected

Also when it tries to release outputFileName the application crashes. Does somebody has any clue what is going wrong? Thanks in advance.

3

3 Answers

8
votes

The problem is an assumption of convention. Specifically, the static analyzer assumes that any method that starts with new returns a retained object. This is because the system APIs follow this convention.

Rename your method; fileNameWithBase:fileNumber: comes to mind.

1
votes

Your method starts with "new" with the analyzer assumes means that you want to return an object with a +1 retain count. Change the name of the method and the warnings should go away.

Also: the method returns an autoreleased object; you shouldn't be releasing it yourself.

0
votes

You're returning an autoreleased string. If you're sending it an additional -release or -autorelease message, your app will crash the next time it tries to access that string.

You should take the time to read the "memory management" section of Apple's developer documentation.