[英]Where am I leaking CGMutablePathRef in this code?
在我的代碼中,我有CGMutablePathRef thePath = CGPathCreateMutable(); 如果我不釋放它,那么我確實會泄漏成千上萬次泄漏,因為我不斷地進行操作。 但是,如果我釋放它,我的游戲最終將崩潰。 有任何想法嗎? 我有可能在錯誤的位置發布它嗎?
-(void)MoveObject:(int)Tag
{
representationX = gameViewObj.spaceshipImageView.center.x;
representationY = gameViewObj.spaceshipImageView.center.y;
CALayer *spaceshipLayer = gameViewObj.spaceshipImageView.layer;
shipAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, representationX, representationY);
statusFire = YES;
BOOL parsedF = NO;
其余代碼如下:
-(void)MoveObject:(int)Tag
{
representationX = gameViewObj.spaceshipImageView.center.x;
representationY = gameViewObj.spaceshipImageView.center.y;
CALayer *spaceshipLayer = gameViewObj.spaceshipImageView.layer;
shipAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, representationX, representationY);
statusFire = YES;
BOOL parsedF = NO;
if(Tag==LeftButtonTag)
{
gameViewObj.spaceshipImageView.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2);
previousButtonTag = LeftButtonTag;
representationX--;
CGPathAddLineToPoint(thePath, NULL,representationX, representationY);
parsedF = YES;
}
else if(Tag==UpButtonTag)
{
gameViewObj.spaceshipImageView.transform = CGAffineTransformMakeRotation(0);
previousButtonTag = UpButtonTag;
representationY--;
CGPathAddLineToPoint(thePath, NULL,representationX, representationY);
parsedF = YES;
}
else if(Tag==RightButtonTag)
{
gameViewObj.spaceshipImageView.transform = CGAffineTransformMakeRotation(M_PI/2);
previousButtonTag = RightButtonTag;
representationX++;
CGPathAddLineToPoint(thePath, NULL,representationX, representationY);
parsedF = YES;
}
else if(Tag==DownButtonTag)
{
gameViewObj.spaceshipImageView.transform = CGAffineTransformMakeRotation(M_PI);
previousButtonTag = DownButtonTag;
representationY++;
CGPathAddLineToPoint(thePath, NULL,representationX, representationY);
parsedF = YES;
}
if(parsedF){
shipAnimation.path = thePath;
shipAnimation.delegate = self;
shipAnimation.duration = 0.003;
[spaceshipLayer addAnimation:shipAnimation forKey:@"position"];
//To kill spaceship when moved backwards.
if(playField[representationX][representationY]==3){
[self doDie];
}
if(playField[representationX][representationY] == 2){
if(onSecretLine){
[gameViewObj.spaceshipImageView setCenter:CGPointMake(representationX, representationY)];
oldPositionX = representationX;
oldPositionY = representationY;
}
}
// case: breaking out
if (playField[representationX][representationY]==0){
if (onSecretLine){
if (statusFire)
{
availableOffline = YES;
oldPositionX=gameViewObj.spaceshipImageView.center.x;
oldPositionY=gameViewObj.spaceshipImageView.center.y;
[gameViewObj DrawLine];
onSecretLine = NO;
availableOffline = NO;
}
}
}
if (playField[representationX][representationY]==0)
if (!onSecretLine)
{
BOOL doIt=true;
// ------------------------------
// prevent contact own line
// ------------------------------
// left
if (Tag==LeftButtonTag) {
if (playField[representationX-1][representationY]==3) {
[self doDie];
doIt=false;
}
}
// right
if (Tag==RightButtonTag) {
if (playField[representationX+1][representationY]==3) {
[self doDie];
doIt=false;
}
}
// up
if (Tag==UpButtonTag) {
if (playField[representationX][representationY-1]==3) {
[self doDie];
doIt=false;
}
}
// down
if (Tag==DownButtonTag) {
if (playField[representationX][representationY+1]==3) {
[self doDie];
doIt=false;
}
}
// special things ...
if (doIt)
{
playField[representationX][representationY]=3;
[gameViewObj DrawLine];
}
}
// case: back to the secure line
if (playField[representationX][representationY]==2)
if (!onSecretLine)
{
[gameViewObj.spaceshipImageView setCenter:CGPointMake(representationX, representationY)];
availableOffline = NO;
onSecretLine = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"FindBirdCenter" object:nil];
for (int i=0; i<[gameViewObj.birdImageViewArray count]; i++) {
UIImageView* ImgBird=[gameViewObj.birdImageViewArray objectAtIndex:i];
int px=ImgBird.center.x;
int py=ImgBird.center.y;
// cristall point
playField[px][py]=5;
}
[self fillPlaygroundExtended];
//Elan function for filling area enclosed
[self fillAreaEnclosed:gameViewObj._myContext];
// invert ..
[self invertPlayground];
// turn the 3 into -> 20+
[self generateNewSecureLine];
}
if(Tag == UpButtonTag){
[self moveShipUp];
}
else if(Tag == RightButtonTag){
[self moveShipRight];
}
else if(Tag == DownButtonTag){
[self moveShipDown];
}
else if(Tag == LeftButtonTag){
[self moveShipLeft];
}
if(doScore == YES){
[self calculateScore];
doScore = NO;
}
[gameViewObj setNeedsDisplay];
}
}
您應該在行后的路徑上執行CGPathRelease()
shipAnimation.path = thePath;
以及針對
if(parsedF){
條件(否則,如果parsedF
為NO,它將仍然泄漏。
您永遠不會釋放thePath
。 至少不在該代碼段中。
Core Graphics使用Core Foundation內存規則 ,您可以在Core Foundation的內存管理編程指南中閱讀該規則 。
簡而言之:名稱中包含Create
任何函數都將返回保留的對象,您將需要使用其名稱中具有Release
的函數來釋放該對象。 如果是CGPathRef
則存在CGPathRelease
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.