开发者

Can someone help with memory leak problem?

开发者 https://www.devze.com 2023-03-19 07:07 出处:网络
I\'m a newbie trying to figure out a memory leak problem. Does anyone see anything wrong with the following code?

I'm a newbie trying to figure out a memory leak problem. Does anyone see anything wrong with the following code?

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:0.10 green:.20 blue:0.30 alpha:1]];
}

In case it helps, Instruments shows the leaked block开发者_运维知识库 with the following info...

Event Type: Malloc

Responsible Library: CoreGraphics

Responsible Caller: CGTypeCreateInstanceWithAllocator

Any help would be greatly appreciated. (I'm still very new, so please explain in as much detail as possible.)

Much thanks!


Bit of a long shot but I'm not sure if it was just a bug in my project or not but I had a similar issue once when setting tintColor in viewWillAppear. I ended up adding an nil check before assigning the color again and this cleared it up for me (I didn't do any further investigation into it so I could be wrong).

if (nil == self.navigationController.navigationBar.tintColor) {
  self.navigationController.navigationBar = [UIColor colorWithRed:0.10 green:.20 blue:0.30 alpha:1];
}


The code you have posted is correct, as far as memory management is concerned.

Instruments Leaks will simply show the point where the leaked object is allocated, not the point where it is actually leaked.

So, your leak is elsewhere. Looking at your code, I would say that the leak is possibly occurring when you release the class containing that code, or elsewhere along the dynamical path to dealloc.

Try commenting out setTintColor to check whether the leak is still there.

Hope this explanation can put you on the right track, but in any case, if you need more help, you should post more code...

0

精彩评论

暂无评论...
验证码 换一张
取 消