views:

341

answers:

1

It's really strange. I have a blank UIImageView subclass that implements the -touchesEnded:, -touchesMoved, and -touchesBegan: methods. The implementations of these methods are empty. They just do nothing. However, when I run Instruments with "Leaks", and I touch the UIImageView and move my finger outside of that UIImageView while still touching the screen, I get an Memory Leak warning from Instruments.

In my demo app there's no object allocation happening when doing that. The methods are empty. Everything I read in Instruments is related to Foundation and Run Loop stuff. I've checked my class twice and removed any object allocation. It's just an skelleton that only shows an image, but that image is not changed when touching it or moving the finger on the screen. That makes no sense.

Did anyone else encounter problems like this?

UPDATE: I testet a little more around and figured out, that memory leaks happen at any spot on the screen when tapping fast around with 5 fingers. Everything I get from Instruments.app is regarding some run and event loops. It seems like if the device can't handle the touches fast enough and then gets stuck at some point with releasing allocated objects. Please try it out and report here if you can see the same problems.

UPDATE: I've tested now a few Apple example apps as well. When I hack with 3 - 5 fingers around on the screen, like a normal user does (yes, they will do!), then Instrument shows up memory leaks regarding event and run loops. Definitely there's a big in the framework, or in instruments. Tested with iPhone OS 2.2.1.

+2  A: 

As reading on an apple forum, it's an unsolved problem in the SDK. It happens when the accelerometer delegate is not nil. Touch event objects are allocated but never freed. The faster the accelerometer delegate is called, the faster those allocation failures happen. Many of the apple sample codes show the same problem. I had the accelerometer turned on.

But I also encountered, that this kind of leaks happen when a touch is tracked from one view onto another. If I keep touching one and the same view and moving my finger on that view without leaving it, I'll not get that problem.

Solutions: Turn accelerometer off (delegate set to nil), reduce the amount of views in your app. I don't know if they fixed that issue in iPhone OS 3.0.

Unfortunately, this will not help:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[UIAccelerometer sharedAccelerometer] setDelegate:nil]; // because of framework bug
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [[UIAccelerometer sharedAccelerometer] setDelegate:self]; // because of framework bug
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [[UIAccelerometer sharedAccelerometer] setDelegate:self]; // because of framework bug
}

More info at: http://discussions.apple.com/thread.jspa?messageID=9396584t

Thanks
the accelerometer is still leaking like hell in 3.0. Thanks for this.
Digital Robot
Hi, I have the same problem, when I tap the screen continuously number of times very quickly. In Leaks it shows the memory allocation is increased by some 16MB.But, when I tap normally like playing a game, it does not show any memory increase in allocation.Is the problem in SDK resolved ? Thank you.
srikanth rongali