I am just cleaning up my code a little bit and I found a few lines in a function where I am not sure if they are producing memory leaks.
I would appreciate any ideas.
Thanks in advance!
In the following code the variables filepath is a NSURL and filename a NSString object defined as properties. The definition attribute is set to “copy”.
Here’s the code :
-(BOOL)isEqual:(id)object
{
if ( self == object)
return YES;
if (!object || ![object isKindOfClass:[self class]])
return NO;
if ( ![[[self filepath] path] isEqual:[[object filepath] path]])
return NO;
if (![[self filename] isEqualToString:[object filename]])
return NO;
if (![[self filesize] isEqualToString:[object filesize]])
return NO;
if (![[[self filepath ] absoluteURL] isEqual:[[object filepath] absoluteURL]])
return NO;
if ( !([[[self filepath ] absoluteURL ] isFileURL ] && [[[object filepath] absoluteURL ] isFileURL ]))
return NO;
return YES;
}
From my understanding, a property with the copy attribute would return an object with a retain count of +1. This would mean, that I should have memory leaks on every if-statement.
Is this correct?
Nothing in your code snippet indicates a leak.
The copy attribute means that you are keeping a copy of the object with a retain count of one when you are setting the property. You cannot leak it simply by referencing it in your code (even via that property’s accessor).
On the other hand, ‘filepath’ and ‘filename’ should be released in the -dealloc method of the object in which they are included. Failure to do that (assuming a reference-count memory mode) will result in leaks.