views:

583

answers:

2
- (id)copyWithZone:(NSZone *)zone {
    PoolFacility *copy = [[[self class] allocWithZone:zone]init];
    copy.name = [self.name copy];
    copy.type = [self.type copy];
    copy.phoneNumber = [self.phoneNumber copy];
    //make sure I get proper copies of my dictionaries
    copy.address = [self.address mutableCopy];  
    copy.webAddress = [self.webAddress copy];
    copy.prices = [self.prices mutableCopy];
    copy.pools = [self.pools mutableCopy];
    return copy;
}

Can anyone see any memory leaks?

Here's the property types:

NSString *name;
NSString *type;
NSMutableDictionary *address;

NSString *phoneNumber;
NSString *webAddress; 

NSMutableArray *prices;
NSMutableArray *pools;

Here are the property declarations:

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSString *phoneNumber;
@property (nonatomic, retain) NSMutableDictionary *address;
@property (nonatomic, copy) NSString *webAddress;
@property (nonatomic, retain) NSMutableArray *prices;
@property (nonatomic, retain) NSMutableArray *pools;
+6  A: 

The properties defined as copy and not retain will have an extra copy when set as below (your code)

copy.name = [self.name copy];
copy.type = [self.type copy];
copy.phoneNumber = [self.phoneNumber copy];
copy.webAddress = [self.webAddress copy];

it should be sufficient to only write them as

copy.name = self.name;
copy.type = self.type;
copy.phoneNumber = self.phoneNumber;
copy.webAddress = self.webAddress;
epatel
But wont this defeat the point of a copy method? I'll be duplicating the class but the variables will point to the same objects? Or will this give me 2 completely independent objects?
Dan Morgan
The "@property (..., copy)" means that a copy is created when you use the dot-notation. The previous value will be sent a release.
epatel
Dan Morgan: The synthesized accessors will copy the objects for you because you declared the properties as `@property(copy)`. If you copy the objects yourself, the accessors will copy those copies. Since you aren't releasing those intermediate copies, they are all leaked.
Peter Hosey
All copied objects will leak. It does not depend on the property type (copy or retain).
Nikolai Ruhe
+1  A: 

This almost certainly leaks like a sieve. You need to provide your @property and other method declarations for us to recommend the best way to fix it.

Mike Abdullah