objective c - Memory leak on calling API using sharedInstance resulting in crashin IOS -
i created sharedinstance
code below:
+ (myclass *)sharedstore { @synchronized(self) { if (sharedinstance == nil) sharedinstance = [[self alloc] init]; } return sharedinstance; }
i have function in myclass
- (myclass *)initservice:(int)servicetype areaselected :(int)areaid target:(id)delegate { self = [super init]; // again init function called after having shared instance resulting in memory leak. if(self) { jsonparserobject = [[myclassinfoparser alloc]init]; if(jsonparserobject != nil) { servicetyperequested = servicetype; selectedareaid = areaid; nsstring *url = [self createurlandbodyforservice]; nslog(@"url : %@",url); if ([reachability connected]) { if(url.length) { bool status = [self initrequest:url withdelegate:delegate]; if(status) nslog(@"request fields successfully"); else nslog(@"failed send request fields"); } else { [super responsefailednotification:nil]; } } else { [super networkerror:delegate]; } } } return self; }
to call above function use code
- (void)startserviceforinformation { serviceobj = [[myclass sharedstore]initservice:2 areaselected:self.areaid target:self]; [serviceobj start]; }
this resulting in memory leak results in crash.can me out fix memory leak?
well, why init method when using singleton class?there no need second init method make method , call singleton instance
try this
- (void)startservice:(int)servicetype areaselected :(int)areaid target:(id)delegate { jsonparserobject = [[myclassinfoparser alloc]init]; if(jsonparserobject != nil) { servicetyperequested = servicetype; selectedareaid = areaid; nsstring *url = [self createurlandbodyforservice]; nslog(@"url : %@",url); if ([reachability connected]) { if(url.length) { bool status = [self initrequest:url withdelegate:delegate]; if(status) nslog(@"request fields successfully"); else nslog(@"failed send request fields"); } else { [super responsefailednotification:nil]; } } else { [super networkerror:delegate]; } } }
and call
- (void)startserviceforinformation { serviceobj = [[myclass sharedstore]startservice:2 areaselected:self.areaid target:self]; [serviceobj start]; }
Comments
Post a Comment