0

As i have finished my iOS App and was removing the memory leaks, I found that instrument was showing leak while assigning the values to it.

In the .h file i have declared the NSdictionary feedData with the property(nonatomic, retain).

I have one more class WebServiceController which returs the NSdictionary.

Here is my code. In the .m file

if ([nid rangeOfString:@","].location == NSNotFound) 
{

    NSArray *data = [[NSArray alloc]initWithObjects:nid,[NSNumber numberWithInt:0],[NSNumber numberWithInt:0],delegate1.myCity,nil];
    WebServiceController *wsct=[[WebServiceController alloc]init];
    wsct.delegate=[[UIApplication sharedApplication]delegate];
    feedData=[wsct getTimelineFeed:data];
    NSLog(@"feed data is--->%@",feedData);
    [wsct release];

}

The code "feedData=[wsct getTimelineFeed:data];" is giving memory leak.

here is the function that is returning the NSDictionary.

-(NSDictionary*)MyTimelineFeed:(NSArray*)data

{

if(![delegate sessionId] )  return nil;    

XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:ENDPOINT]];





NSMutableArray * postParams = [NSMutableArray array];

NSString * method = [NSString stringWithFormat:@"timeline.getFeedItems"];

[postParams addObject:[delegate sessionId] ];  





for (int i = 0;  i < [data count]; i++) {

    [postParams addObject:[data objectAtIndex:i]];

}



[request setMethod:method withObject:postParams];

XMLRPCResponse *nodeSaveResponse = [XMLRPCConnection sendSynchronousXMLRPCRequest:request];


[request release];  


if([nodeSaveResponse isKindOfClass:[NSError class]])

    return nil;


NSMutableDictionary * dict = [nodeSaveResponse object];

if ([dict isKindOfClass:[NSError class]]) {
    //NSLog(@"Error found");
    return nil;
}

return dict;

}

pls help.

Ajeet Pratap Maurya
  • 4,244
  • 3
  • 28
  • 46

3 Answers3

2

I am assuming that this method:

feedData=[wsct getTimelineFeed:data];

Is returning a NSDictionary with a retain count of 1, which should not be the case, because according to the memory management rules for Cocoa only methods that start with new or alloc or contain copy should return objects with a retain count of 1.

Change your method to this:

newTimelineFeed

And it should be good to go :)

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • Why there is a need to change the function name.what if i change it to MyTimelineFeed, as the NSDictionary returned by the function is saved by the object feedData, for which i already declare its property(nonatomic,retain). – Ajeet Pratap Maurya Nov 01 '11 at 06:42
  • @AjeetPratapMaurya That is because if the method returns a retained object the memory management rules for Cocoa indicate it should be named that way. Additionally although your property feedData is retain you are accessing the ivar directly and not thru the setter, to use the setter you must use self.feedData = newData; – Oscar Gomez Nov 01 '11 at 12:41
0

You should be using the line

self.feedData =[wsct getTimelineFeed:data]; 

Not

feedData =[wsct getTimelineFeed:data]; 

I can't see any leaks in the rest of the code but there is potential here to lose a reference to a previously allocated variable as you are not going through the synthesized accessor - using self.feedData will release any previous value held in the ivar before retaining the new one, assigning a value to the ivar directly will not do this.

jrturton
  • 118,105
  • 32
  • 252
  • 268
-2

You can change the line:

feedData = [wsct getTimelineFeed:data];

With:

feedData = [[wsct getTimelineDeed:data] retain];

Just remember to release feedData when it is no longer needed.

Patrizio
  • 1
  • 2
  • I guess the "very right" answer would have been to consider using [self.feedData release]; self.feedData = [wsct getTimelineFeed:data]; [data release]; as the property has been defined as retain, so why don't you just use it. Also don't forget to release the feedData value during the class dealloc – Seb T. Oct 31 '11 at 13:34
  • 1
    @SebastienThuilliez - no, that isn't "very right". See http://stackoverflow.com/questions/7262268/why-shouldnt-i-use-the-getter-to-release-a-property-in-objective-c/7262360#7262360 for an excellent discussion – jrturton Nov 01 '11 at 08:28
  • 1
    Extra retains aren't going to stop memory _leaks_ – jrturton Nov 01 '11 at 08:30