Birdey
Birdey

Reputation: 5

Unable to detect cause of memory leak

In this function i have a memory leak on two locations according to Xcode's Instrument leak tool.

I don't have ARC enabled and wont be able to activate it, so i have to manage this in another way.

+(NSArray *)changePolygonFrom:(NSArray *)polygon size:(float)size{

    int nmrOfPoints = [polygon count];
    double x[nmrOfPoints];
    double y[nmrOfPoints];
    for (int i = 0; i < nmrOfPoints; i++) {
        CGPoint p = [[polygon objectAtIndex:i] CGPointValue];
        x[i] = p.x;
        y[i] = p.y;
    }

    insetPolygon(x,y,nmrOfPoints,size);

    NSMutableArray *rPolygon = [[NSMutableArray alloc] initWithCapacity:nmrOfPoints];
    for(int i = 0; i < nmrOfPoints; i++){
        CGPoint p = CGPointMake(x[i], y[i]);
        NSValue *val = [NSValue valueWithCGPoint:p];
        [rPolygon addObject:val];
    }

    return rPolygon;
}

This is where i call it

-(void)fixFuzArrays{
    if (fuzzFix) {
        largeFuzzPolygon = [Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/2)];
        smallFuzzPolygon = [Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/6)];
    }else{
        largeFuzzPolygon = [Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/2)];
        smallFuzzPolygon = [Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/6)];
    }
}

If i use Auto release on the rPolygon i get a BAD_ACCESS on the objects iFrame and oFrame witch are the returned products of the previous function

-(void)drawAreaFuzzWithOuterFrame:(NSArray *)oFrame andInnerFrame:(NSArray *)iFrame withColor:(ccColor4B)c{
    for(int i = 0; i < [oFrame count]; i++){

        CGPoint oP1 = [[oFrame objectAtIndex:i] CGPointValue];
        CGPoint iP1 = [[iFrame objectAtIndex:i] CGPointValue];

        CGPoint oP2;
        CGPoint iP2;

        if(i == [oFrame count]-1){
            oP2 = [[oFrame objectAtIndex:0]   CGPointValue];
            iP2 = [[iFrame objectAtIndex:0]   CGPointValue];
        }else{
            oP2 = [[oFrame objectAtIndex:i+1]   CGPointValue];
            iP2 = [[iFrame objectAtIndex:i+1]   CGPointValue];
        }

        GLfloat vertices[] = {
            oP1.x,oP1.y,
            oP2.x,oP2.y,
            iP1.x,iP1.y,
            iP2.x,iP2.y,
        };

        GLubyte colors[] = {
            c.r,c.g,c.b,c.a,
            c.r,c.g,c.b,c.a,
            c.r,c.g,c.b,0,
            c.r,c.g,c.b,0,
        };

        BEdrawGLShapeWithVertices(vertices, colors);
    }
}

Upvotes: 0

Views: 96

Answers (3)

Marco Pace
Marco Pace

Reputation: 3870

You forgot an autorelease while declaring rPolygon:

NSMutableArray *rPolygon = [[[NSMutableArray alloc] initWithCapacity:nmrOfPoints] autorelease];

EDIT It seems that you are not retained the result of the +(NSArray *)changePolygonFrom:(NSArray *)polygon size:(float)size method.

Try to change your code retaining largeFuzzPolygon and smallFuzzPolygon (remember to release them in the dealloc method too):

-(void)fixFuzArrays{
    [largeFuzzPolygon release];
    [smallFuzzPolygon release];

    if (fuzzFix) {
        largeFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/2)] retain];
        smallFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/6)] retain];
    }else{
        largeFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/2)] retain];
        smallFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/6)] retain];
    }
}

or better I suggest you to synthesize them and use this code:

-(void)fixFuzArrays{
    if (fuzzFix) {
        self.largeFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/2)] retain];
        self.smallFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/6)] retain];
    }else{
        self.largeFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/2)] retain];
        self.smallFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/6)] retain];
    }
}

Upvotes: 0

Hani Ibrahim
Hani Ibrahim

Reputation: 1449

You have to autorelease the array before returning it and then retain the array in the method that is receiving it

add autorelease to NSMutableArray

NSMutableArray *rPolygon = [[[NSMutableArray alloc] initWithCapacity:nmrOfPoints] autorelease];

and then put retain on the method that use your function but you will have to release it later

NSArray *myArray = [[MyClass changePolygonFrom:array size:size] retain];

Don't forget to release myArray later

Upvotes: 1

Antonio MG
Antonio MG

Reputation: 20410

You have to mark the NSMutableArray as autorelease:

NSMutableArray *rPolygon = [[[NSMutableArray alloc] initWithCapacity:nmrOfPoints] autorelease];

If you don't do it, you are creating it, retaining it, and then returning it. It's never released.

Upvotes: 0

Related Questions