PDA

View Full Version : Can any one identify the leak???




bluehill
May 2, 2011, 09:38 AM
The performance tool is showing leak at this position( red color).

NSString *street = [[NSString alloc] initWithString:[dictContact objectForKey:@"add"]];
NSString *city = [[NSString alloc] initWithString:[dictContact objectForKey:@"city"]];
NSString *state = [[NSString alloc] initWithString:[dictContact objectForKey:@"state"]];
NSString *pin = [[NSString alloc] initWithString:[dictContact objectForKey:@"pin"]];

NSMutableString *address = [[NSMutableString alloc] initWithFormat:@"%@+%@+%@+%@",street,city,state,pin];
[address replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [address length])];

[street release];
[city release];
[state release];
[pin release];

//NSLog(@"Address %@",address);
MapViewController *mvController = [[MapViewController alloc]init];
[mvController setAddress:address];
[self.navigationController pushViewController:mvController animated:YES];
[mvController release];

[address release];



PhoneyDeveloper
May 2, 2011, 12:10 PM
The leaks tool doesn't tell you where leaks happened. It tells you where leaked objects were created. The red line is where the leaked string was created.

There's no obvious bug in the code shown. Does anything else leak? Do you release the address in the MapViewController dealloc method? Does the MapViewController leak?

bluehill
May 3, 2011, 09:13 AM
The leaks tool doesn't tell you where leaks happened. It tells you where leaked objects were created. The red line is where the leaked string was created.

There's no obvious bug in the code shown. Does anything else leak? Do you release the address in the MapViewController dealloc method? Does the MapViewController leak?





@interface MapViewController : UIViewController <MKMapViewDelegate,NSXMLParserDelegate>
{
IBOutlet MKMapView *mapView;
NSURLConnection *updateConnection;
NSMutableData *updateData;
NSData *geoData;

AddressAnnotation *addAnnotation;

NSString *address;

NSMutableDictionary *geoLoc;
NSMutableString *workingPropertyString;

}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
.....



When checked the retaincount of mapView it showed count 2 and i had released it only once in dealloc coz of which it was leaking...and now i have released it twice and working fine

why is it showing count 2? Is it the IB has already an instance with it.

When i donot create a property then also it show retaincount 2 but if i release it twice then it crashes.!!!!!:confused:

PhoneyDeveloper
May 3, 2011, 10:21 AM
I'm not clear on your explanation. You should always have a property for IBOutlets. You should release the ivar related to the property in dealloc. You shouldn't add an extra release. Is this what you have?

dejo
May 3, 2011, 10:36 AM
And to quote Phoney from a previous thread:

retainCount is often not useful for debugging memory leaks. There are too many objects that work behind the scenes that retain objects and there are also cases where an object is both retained and autoreleased to control the lifetime of the object. It's difficult or impossible to understand all these cases because you don't see all the code.

What you should do is learn the memory management rules. Then use them correctly. Then use build and analyze and the leaks tool to find problems.

bluehill
May 4, 2011, 05:37 AM
You shouldn't add an extra release. Is this what you have?
Yes




@synthesize mapView;

- (void)dealloc
{
//NSLog(@"Retain cont address=%d geoLoc=%d workingPropertyString=%d mapView=%d",[address retainCount],[geoLoc retainCount],[workingPropertyString retainCount],[mapView retainCount]);
[updateData release];
[updateConnection release];

[address release];
[geoLoc release];
[workingPropertyString release];
[mapView release];
[super dealloc];
}


I deleted the app from the simulator and again Run it with Performance tool now it is not showing any leaks with same old code.... :confused:

Now mapView released only once...