1

My app is receiving memory warnings because it's asking for lot of memory. I try to release every allocation. However, sometimes I don't know how to do it.

For example: I have two pairs of .h and .m file. One of them makes connections with a server and the other with local SQLite.

Usually, the code which calls to a method from those files are like this:

-(NSMutableArray *) getRecentActivity{
    LocalStorageController *local = [[LocalStorageController alloc]init];
    return [local getRecentActivity];
}

getRecentActivity returns a NSMutableArray.

Well, in that piece of code we can see that I am allocating memory for the LocalStorageController but I never call to the release method so, I suppose, the more I call that function, the more memory I will be allocating.

If I call autorelease after init, it will crash.

Moreover, usually, I use this other kind of code:

    ServerConnection *serv = [[ServerConnection alloc]init];
    NSMutableArray list = [serv getMyListOfContacts];

Which uses ASIHTTPRequest and, if I call [serv release]; after the second line, the app crashes with EXC_BAD_ACCESS pointing to a line in ASIHTTPRequest library.

How is suppose to manage this situation?

Thank you very much!

Ibai
  • 568
  • 1
  • 7
  • 21

2 Answers2

2

The first case is easy;

-(NSMutableArray *) getRecentActivity{
    LocalStorageController *local = [[LocalStorageController alloc]init];
    NSMutableArray *tmp = [local getRecentActivity];
    [local release];
    return tmp;
}

The second case is hard to solve in a general way without seeing more of the actual code.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • 2
    You'd also want a retain on `tmp` and then return it `autorelease`d. You can't assume `tmp` is valid after the `[local release]` unless you do that. – mattjgalloway Feb 05 '12 at 20:24
0

Using serv as a property would be fixing this retain/release problem.

In your .h:

@property (nonatomic, retain) ServerConnection *server;

In your .m:

@synthesize server;

- (void)dealloc {
    [server release];
    // The rest of your releases here...
    [super dealloc];
}

- (void)yourMethod {
    ServerConnection *myServConnection = [[ServerConnection alloc] init];
    self.serv = myServConnection;
    [myServConnection release];
    NSMutableArray list = [self.serv getMyListOfContacts];
}

Just keep on using self.serv in this class from that point on and you won't have a problem with having the object being released.

Simon Germain
  • 6,834
  • 1
  • 27
  • 42
  • I was simply showing where the code was going for the release, but you're right. I updated my answer. Thanks for pointing that out! – Simon Germain Feb 05 '12 at 20:23
  • Edit it again, you should put the call to super dealloc at the bottom. – Till Feb 05 '12 at 20:33
  • First, thanks for the answer. I realized that my example was wrong: It seems that I can't release it because I'm using ASIHTTPRequest asynchronously so it needs to stay there for the delegate. If I do that, it crashes again... So, how would you do it now? – Ibai Feb 05 '12 at 21:43
  • Use it as a property but initialize it in your - (id)init function. – Simon Germain Feb 05 '12 at 22:04
  • I've put it that way but it causes the same error. I think that as the answer can arrive several seconds later, if I release the Server, it can't treat the response because the release comes first and so, the ASIHTTPRequest can't call the delegate as it has been released. I don't know if I'm explaining well... But if I do that way it crashes and if I do nothing, it will retain in memory, isn't it? – Ibai Feb 05 '12 at 22:18
  • You'd have to update your question with what you've tried so far. It's hard to pinpoint an error when you can't see what you're working with. – Simon Germain Feb 05 '12 at 22:33