Trying to make sense of a reported leak in instruments
Instruments reports I have a memory leak with the first line of the code. But as you can see at the bottom of the code I release the flipcoin object. I have no other alloc on that line so I don't understand what could be the problem? I'm guessing there is a part of memory management that I开发者_如何学Python have misunderstood, can somebody give me a hint as to what can be causeing this reported leak?
flipCoin= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0000.png"]];
CGRect frameX;
UIImageView *coinFlipImage = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:@"0000.png"]];
frameX = coinFlipImage.frame;
frameX.origin.x = (480/2) - (frameX.size.width/2);
frameX.origin.y = (320/2) - (frameX.size.height/2);
[flipCoin initWithFrame:frameX];
flipCoin.animationImages = myImages;
flipCoin.animationDuration = 1.4; // seconds
flipCoin.animationRepeatCount = 1; // 0 = loops forever
//[flipCoin startAnimating];
[self.view addSubview: flipCoin];
[coinFlipImage release];
[flipCoin release];
Many Thanks -Code
You are doing an init on flipCoin twice.
Once here:
flipCoin= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0000.png"]];
And then here:
[flipCoin initWithFrame:frameX];
Instead of that second instance just set the frame like so:
flipCoin.frame = frameX;
If you replace
[flipCoin initWithFrame:frameX];
with
[flipCoin setFrame:frameX];
does it solve the problem ?
精彩评论