2

green hand i am. I'm using instruments, and it did a great help to me so far, but I'm confused now 'cause it report a memory leak to me while its leaked block history shows me that the ref count of that memory had finally become 0. What does it mean?
It's really embarrassing that I couldn't post a image here... so I have to describe it in text. Hope it would be clear enough for you:

Event Type || RefCt || Responsible Library || Responsible Caller
Malloc         || 1        || MyWeather              || +[ForecastData parseSingleForecastWithXMLElement:]
Autorelease||           || MyWeather              || +[ForecastData parseSingleForecastWithXMLElement:]
Retain         || 2        || MyWeather              || +[ForecastData parseWithData:]
Release      || 1        || Foundation              || +[NSAutoreleasePool drain:]
Retain         || 2        || Foundation              || +[NSThread initWithTarget:selector:object:]
Release      || 1        || Foundation              || +[NSString compare:options:]
Release      || 0        || MyWeather              || +[RootViewController dealloc]

Any help will be appreciated~

bromj
  • 61
  • 4

3 Answers3

4

It was caused by the lack of [super dealloc] in dealloc of forecastData, so that part of memory of forecastData is never freed while the retain count of forecastData did have become zero. Anyway, thanks guys.

bromj
  • 61
  • 4
0

you're not providing much sample code so it could be anything. The RefCount of MyWeather is zero but Foundation is still one, so maybe you have anywhere allocated a NSSting an never released?

btw. I would never alloc-init a string, instead setting the text directly and let the memory management do the rest. I don't know why but I think it's a little buggy. Sometimes I get strange errors if I try something like that:

NSString *str = [[NSString alloc] initWithString:@"some Text"];
myLabel.text = str;
[str release];

myLabel should retain it but it doesn't. I will get an error if I try to release it. (and a leak if not)

If I use

NSString *str = @"some Text";
myLabel.text = str;

it works great, no error and no leak.

  • I'm sorry for lack of sample code 'cause I'm not in my lab now, and actually I still have no idea how to present those related code appropriately. However, I'll try. Advice about NSString is great, and I'll take it into consideration. Thanks a lot~ – bromj Dec 19 '10 at 06:42
0

Did you try this on the device? Sometimes you can see leaks show up that are not really leaks.

The other reason could have been that you had NSZombie enabled, which means objects don't really get released.

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • I haven't try this on the device yet. And NSZombie is actually a new term to me, so I think I should do some google and then decide whether it could be the reason. Anyway, thanks for your answer~ – bromj Dec 19 '10 at 06:32
  • 1
    If you're not sure what NSZombie is, you probably do not have it on. But I would try on the device. – Kendall Helmstetter Gelner Dec 19 '10 at 07:06
  • I haven't got my iDUP account so I could not try it on the device now. However, I wonder that is it possible to be caused by a multithread environment? – bromj Dec 19 '10 at 07:54
  • Threads are not usually the issue with leaks, and that call stack doesn't really look like threads are at work much. – Kendall Helmstetter Gelner Dec 20 '10 at 07:21
  • 1
    I got it finally... It was caused by the lack of [super dealloc] in dealloc of forecastData, so the retain count of forecastData did become zero, but memory of its parent isn't freed at all. Thanks for all your advice~ – bromj Dec 21 '10 at 06:42
  • I see, an excellent point - release lowers the retain count but without dealloc being finished the object still exists. Very interesting, thanks for posting the solution. – Kendall Helmstetter Gelner Dec 21 '10 at 20:27