Memory leak every time UIScrollView is released

2019-01-09 09:30发布

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:

4条回答
祖国的老花朵
2楼-- · 2019-01-09 09:55

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.

查看更多
走好不送
3楼-- · 2019-01-09 09:55

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.

查看更多
叼着烟拽天下
4楼-- · 2019-01-09 09:56

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.

查看更多
SAY GOODBYE
5楼-- · 2019-01-09 09:56

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];
}
查看更多
登录 后发表回答