English 中文(简体)
重放 CGMuablePathRef 存在问题
原标题:Releasing CGMutablePathRef is problematic

我得到hexTouchAreas 作为我的方法 -drawHexagonTouchArea 的返回值,但是当我分析我的工程时,它写着CGMutablePathRef hexTouchArea = CGPathCreateMubable (); 产生警告 : “ 在HexTouchArea 初始化过程中保存到它的价值从未读读过 。 ” 在写着 CGPathRelease (hexTouchArea) 的行内, 它抱怨我并不拥有该对象, 释放它也是多余的 。

另一个警告是在线 return path; 上,分析器说:“在629线上分配并存储到路径的物体(即CGMubablePathRef path = CGPathCreate Mutabable (); )可能泄漏”

-(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;
}
问题回答

使用以下代码,您将创建可变路径, 将其指定为 HexTouchArea, 然后用另一个已创建的天体覆盖 HexTouchArea 。

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

您可能想要使用的是以下内容。

CGMutablePathRef hexTouchArea = [self drawHexagonTouchArea:touchAreaOrigin];




相关问题
Weak event handler model for use with lambdas

OK, so this is more of an answer than a question, but after asking this question, and pulling together the various bits from Dustin Campbell, Egor, and also one last tip from the IObservable/Rx/...

JavaScript memory problem with canvas

I m using getImageData/putImageData on a HTML5 canvas to be able to manipulate a picture. My problem is that the browser never seems to free any memory. Not until I close the tab (tested in Chrome and ...

Memory leak for CComBSTR

I have read that the following code causes memory leak. But did not understand why. CComBSTR str; pFoo->get_Bar(&str); pFoo->get_Baf(&str); How does it cause a leak when we are not ...

Hunting memory leaks

I m finding leaked heap blocks by using the following command in WinDbg !heap –l With each leaked heap block I get, I m running to following to get the stack trace. !heap -p -a leakedheapblock The ...

NSScanner memory leak

I m at my first experiences with iPhone development. I wrote some basic code to test the NSScanner class, and now I was looking into the Leaks tool. It seems that this code is leaking, when in the ...

Do Small Memory Leaks Matter Anymore?

With RAM typically in the Gigabytes on all PC s now, should I be spending time hunting down all the small (non-growing) memory leaks that may be in my program? I m talking about those holes that may ...

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...