当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS 常见内存泄漏以及解决方案

IOS 常见内存泄漏以及解决方案

2019年07月24日  | 移动技术网IT编程  | 我要评论

织金县人民政府,营口新闻,4921kb

ios 常见内存泄漏以及解决方案

整理了几个内存泄漏的例子,由于转载地址已经找不到了,在这里就不一一列出来了。
1 oc和cf转化出现的内存警告

cfstringref cfstring = cfurlcreatestringbyaddingpercentescapes(kcfallocatordefault,(cfstringref)picdatastring,null,cfstr(":/?#[]@!$&'()*+,;="),kcfstringencodingutf8);

nsstring *basestring = [nsstring stringwithstring:(nsstring *)cfstring];

//释放
cfrelease(cfstring);

2,循环参照

a有个属性参照b,b有个属性参照a,如果都是strong参照的话,两个对象都无法释放。

这种问题常发生于把delegate声明为strong属性了。

例,

@interface sampleviewcontroller

@property (nonatomic, strong) sampleclass *sampleclass;

@end

@interface sampleclass

@property (nonatomic, strong) sampleviewcontroller *delegate;

@end

上例中,解决办法是把sampleclass 的delegate属性的strong改为assing即可。

3,死循环

如果某个viewcontroller中有无限循环,也会导致即使viewcontroller对应的view关掉了,viewcontroller也不能被释放。

这种问题常发生于animation处理。

例,

比如,

catransition *transition = [catransition animation];

transition.duration = 0.5;

tansition.repeatcount = huge_vall;

[self.view.layer addanimation:transition forkey:"myanimation"];

上例中,animation重复次数设成huge_vall,一个很大的数值,基本上等于无限循环了。

解决办法是,在viewcontroller关掉的时候,停止这个animation。

-(void)viewwilldisappear:(bool)animated {

  [self.view.layer removeallanimations];

}

内存泄露的情况当然不止以上两种。

感谢阅读,希望能帮助到大家,谢谢大家对本站 的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网