Home>

IOS common memory leaks and solutions

1Memory warning in oc and cf conversion

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

2, circular reference

a has an attribute reference b, b has an attribute reference a, if both are strong references,Neither object can be released.

This problem often occurs when the delegate is declared as a strong attribute.

example,

@interface sampleviewcontroller
@property (nonatomic, strong) sampleclass * sampleclass;
@end
@interface sampleclass
@property (nonatomic, strong) sampleviewcontroller * delegate;
@end

In the above example, the solution is to change the strong of the delegate property of sampleclass to assing.

3. Endless loop

If there is an infinite loop in a viewcontroller,It will also cause that the viewcontroller cannot be released even if the view corresponding to the viewcontroller is closed.

This kind of problem often occurs in animation.

example,

such as,

catransition * transition=[catransition animation];
transition.duration=0.5;
tansition.repeatcount=huge_vall;
[self.view.layer addanimation:transition forkey:"myanimation"];

In the above example, the number of animation repetitions is set to huge_vall, a very large value.Basically equal to infinite loop.

The solution is,When the viewcontroller is turned off,Stop this animation.

-(void) viewwilldisappear:(bool) animated {
  [self.view.layer removeallanimations];
}

Of course, there are more than two kinds of memory leaks.

ios
  • Previous Distributed deployment of pyspider in centos7
  • Next Python to determine if a variable is a string in Json format
  • Trends