问题
In my app I have a scroll view and four table views. Each time one is dragged and then released, I get a 48 byte leak. This really adds up. As you can see, both groups of leaks have the same source. Has anyone seen a leak like this before?
Edit 1:
When I click on the arrow next to the leak, I get this information for the leak:
回答1:
What you are seeing is a known bug in iOS 5.1 and is being discussed in the iOS Developer Forums as such. You can find the relevant thread by searching in the forums for "strdup". See the thread titled "Elements App Memory Leak". Search for the post from an Apple employee.
回答2:
Most likely, it's your fault, somehow.
In the Allocations instrument, press the "i" button and turn on "Record Reference Counts". Then Instruments can show you all the allocation, retain, autorelease, and release events that happened to those objects. (You should see an arrow next to each leaked item -- click it to show the allocation history of that object.)
I think you'll find that some of your code is retaining something, or indirectly causing it to be retained. Probably the scroll view or one of its gesture recognizers, as a guess.
回答3:
A workaround:
I realised that somehow this leaked bytes are stored within the scrollview. You have to release your scrollview and alloc it again from time to time, keeping its state. The way you detect when you should reload the scrollview is up to you, depends on your app needs. Every time you release the scrollview, these bytes are also released.
回答4:
Workaround: I found that the memory leak occurred in handlePan: if the UIScrollView delegate is set. I needed the delegate methods, so I subclassed UIScrollView, and declared my own @protocol. Then I overrode the target selector for the scrollView panGestureRecognizer, without sending it to super:
//yourScrollView.h
@protocol yourScrollViewDelegate
-(void)yourProtocol;
@end
//yourScrollView.m
-(void)handlePan:(id)sender{
[yourDelegate yourProtocol];
}
来源:https://stackoverflow.com/questions/9762995/memory-leak-every-time-uiscrollview-is-released