Trond Kristiansen
Trond Kristiansen

Reputation: 2446

Memory leak in cocoa code

I am stuck trying to analyze why Xcode analyzer (v4.2) complaints that my objective-c code leaks memory. I am creating a NSOperation that 1) recursively creates a directory if it does not exists, 2) copies a file from one directory to another into that directory.

The NSOperation is initialized with:

- (id)initWithFullPathSource:(NSString *) mysource andFullPathCopy:(NSString *) mycopy     andNewDirectory:(NSString *) mydir withMode:(NSString *) mymode withLR:(NSString *) LR
{

     self = [super init];
     if (self) {

        [self setFullPathSource:mysource];
        [self setFullPathCopy:mycopy];
        [self setNewDirectory:mydir];
        [self setMode:mymode];
        [self setMyLR:LR];
    }

    return self;
}

Attached is a screenshot of how Xcode analyzer sees my code. Could anyone please help me understand why I am leaking memory in this case? I am releasing 'createComponents' and 'removeComponents' at the end of the main routine so I thought I was in the clear.

Hope someone can shed some light on my problem. Thanks! Cheers, Trond

enter image description here

Upvotes: 0

Views: 180

Answers (1)

danielbeard
danielbeard

Reputation: 9149

Be aware that under the objective c naming rules, a method that starts with the word new or copy is assumed to return an object that is owned by the caller. If the call to [self newDirectory] returns an autoreleased object, then try renaming it so it doesn't begin with new.

There is a great answer explaining more here: Semantic Issue: Property's synthesized getter follows Cocoa naming convention for returning 'owned' objects

A suggestion would be to either change the name of the getter on the property like so:

@property (strong, nonatomic, getter=theNewDirectory) NSString *newDirectory;

Or call it something like : directoryToBeCreated

Upvotes: 7

Related Questions