2012-05-25 35 views
0

我得到hexTouchAreas我的方法-drawHexagonTouchArea的返回值,但是當我分析我的項目,它說:CGMutablePathRef hexTouchArea = CGPathCreateMutable();該生產線生產的警告:「它的初始化過程中存儲到‘hexTouchArea’值沒讀過」 。並且在CGPathRelease(hexTouchArea);這條線上,它抱怨說我不擁有這個對象,並且釋放它是多餘的。釋放CGMutablePathRef是有問題的

另一個警告是在線路return path;其中分析器說:「上線629分配的物體的潛在的泄漏並存儲到路徑(這是CGMutablePathRef path = CGPathCreateMutable();)」

-(void)createTouchAreas{ 

    int realPositionsCount =[[GameStateSingleton sharedMySingleton]getSharedRealCountOfPositions]; 
    hexTouchAreas = [[GameStateSingleton sharedMySingleton]getSharedHexTouchAreas]; 
    NSMutableDictionary *existingHexagons = [[GameStateSingleton sharedMySingleton]getExistingHexagons]; 
    for (int i = 0; i <= realPositionsCount;i++){ 
     //create touchareas 
     NSString *hexKey = [NSString stringWithFormat:@"hexagon%d", i]; 
     CCSprite *theSprite = [[existingHexagons objectForKey:hexKey]objectForKey:@"realSprite"]; 
     CGPoint touchAreaOrigin = ccp(theSprite.position.x -22, theSprite.position.y-40); 
     NSString *touchAreaKey = [NSString stringWithFormat:@"hexTouchArea%d",i]; 
     CGMutablePathRef hexTouchArea = CGPathCreateMutable(); 
     hexTouchArea = (CGMutablePathRef) [self drawHexagonTouchArea:touchAreaOrigin]; 
     [hexTouchAreas setObject:(id)hexTouchArea forKey:touchAreaKey]; 
     [[GameStateSingleton sharedMySingleton]setSharedHexTouchAreas:hexTouchAreas]; 
     CGPathRelease(hexTouchArea); 

    } 



} 

方法創建並返回的路徑:

-(CGMutablePathRef)drawHexagonTouchArea:(CGPoint)origin 
{ 


    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPoint newloc = CGPointMake(origin.x, origin.y); 

    CGPathMoveToPoint(path, NULL, newloc.x, newloc.y); 
    CGPathAddLineToPoint(path, NULL, newloc.x -22,newloc.y + 38); 
    CGPathAddLineToPoint(path, NULL, newloc.x + 0, newloc.y + 76); 
    CGPathAddLineToPoint(path, NULL, newloc.x + 46, newloc.y + 76); 
    CGPathAddLineToPoint(path, NULL, newloc.x +66,newloc.y + 40); 
    CGPathAddLineToPoint(path, NULL, newloc.x +44, newloc.y + 0); 
    CGPathCloseSubpath(path); 
    return path; 
} 

回答

1

用以下代碼創建一個可變路徑,將其分配給hexTouchArea,然後用另一個創建的對象覆蓋hexTouchArea。

CGMutablePathRef hexTouchArea = CGPathCreateMutable(); 
hexTouchArea = (CGMutablePathRef) [self drawHexagonTouchArea:touchAreaOrigin]; 

你可能想要使用的是以下內容。

CGMutablePathRef hexTouchArea = [self drawHexagonTouchArea:touchAreaOrigin];