objective c - Iphone sdk, memory leak -
objective c - Iphone sdk, memory leak -
im new objective-c. have problem memory leaking when developing iphone app. leaking utility in xcode shows leaking problem 'combarr'->'results' object. there function parsing json url , returns nsarray:
- (nsarray *)getlisting2:(nsstring *)item from:(int)country { //sending post request params nsstring *post = [@"product=" stringbyappendingstring:item]; nsstring *countrystr = [nsstring stringwithformat:@"&country=%d", country]; post = [post stringbyappendingstring:countrystr]; nsdata *postdata = [post datausingencoding:nsutf8stringencoding allowlossyconversion:yes]; nsstring *postlength = [nsstring stringwithformat:@"%d", [postdata length]]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; nsuserdefaults *prefs = [nsuserdefaults standarduserdefaults]; nsstring *url = [prefs objectforkey:@"urltoapi"]; url = [url stringbyappendingstring:@"/get-items/"]; [request seturl:[nsurl urlwithstring:url]]; [request sethttpmethod:@"post"]; [request setvalue:postlength forhttpheaderfield:@"content-length"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; [request sethttpbody:postdata]; nsdata *response = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil]; [request release]; //receiving json nsstring *jsonstring = [[nsstring alloc] initwithdata:response encoding:nsutf8stringencoding]; sbjsonparser *json = [[sbjsonparser alloc] init]; nserror *error = nil; //parsing json nsdictionary nsdictionary *results = [[nsdictionary alloc] initwithdictionary:[json objectwithstring:jsonstring error:&error]]; [json release]; [jsonstring release]; //generate array of items nsmutablearray *listofitems = [[nsmutablearray alloc] init]; (int = 0; < [[results objectforkey:@"data"] count]; i++) { [listofitems addobject:[[results objectforkey:@"data"] objectforkey:[nsstring stringwithformat:@"%d", i]]]; } //saving items array , count info object 1 array nsarray * returnarr = [[[nsarray arraywithobjects:listofitems, [results valueforkey:@"count_info"], nil] retain] autorelease]; [listofitems release]; [results release]; homecoming returnarr; }
and executing function here:
myapi *itemsapi = [[myapi alloc] init]; nsarray *combarr = [[izdapi getlisting2:item from:countryid] retain]; [itemsapi release]; listofitems = [[combarr objectatindex:0] retain]; if([listofitems count] > 0){ pricearr = [[combarr objectatindex:1] retain]; } else{ totalcount = 0; } [combarr release];
thank helping
every time allocate memory, must release it. (alloc, copy, retain). releasing myapi
, not itemsapi
. seek this...
myapi *itemsapi = [[itemsapi alloc] init]; nsarray *combarr = [[izdapi getlisting2:item from:countryid] retain]; [itemsapi release]; listofitems = [[combarr objectatindex:0] retain]; if([listofitems count] > 0){ pricearr = [[combarr objectatindex:1] retain]; } else{ totalcount = 0; } [combarr release];
if using xcode 4, seek turning on arc. in short, arc handles releasing of memory. little burden off shoulders , 1 less thing worry about.
iphone objective-c xcode memory-leaks
Comments
Post a Comment