2010-11-13 25 views
4

J'ai rendu près de 1000 polygones dans la carte. Je reçois le chemin du polygone en utilisantFuite de mémoire énorme dans CGMutablePathRef

- (CGPathRef)polyPath:(MKPolygon *)polygon 
{ 
    MKMapPoint *points = [polygon points]; 
    NSUInteger pointCount = [polygon pointCount]; 
    NSUInteger i; 
    if (pointCount < 3) 
     return NULL; 
    CGMutablePathRef path = CGPathCreateMutable(); 
    if([polygon isKindOfClass:[MKPolygon class]]) 
    { 
      for (MKPolygon *interiorPolygon in polygon.interiorPolygons) 
     { 
     CGPathRef interiorPath = [self polyPath:interiorPolygon]; 
     CGPathAddPath(path, NULL, interiorPath); 
     CGPathRelease(interiorPath); 
     } 
    } 
    CGPoint relativePoint = [self pointForMapPoint:points[0]]; 
    CGPathMoveToPoint(path, NULL, relativePoint.x, relativePoint.y); 
    for (i = 1; i < pointCount; i++) 
    { 
      relativePoint = [self pointForMapPoint:points[i]]; 
      CGPathAddLineToPoint(path, NULL, relativePoint.x, relativePoint.y); 
    } 
    return path; 
} 

- (void)drawMapRect:(MKMapRect)mapRect 
     zoomScale:(MKZoomScale)zoomScale 
     inContext:(CGContextRef)context 
{ 
    MultiPolygon *multiPolygon = (MultiPolygon *)self.overlay; 
for (MKPolygon *polygon in multiPolygon.polygons) 
{ 
    if([polygon isKindOfClass:[MKPolygon class]]) 
    { 
      CGPathRef path = [self polyPath:polygon]; 
      if (path) 
      { 
       [self applyFillPropertiesToContext:context atZoomScale:zoomScale]; 
       CGContextBeginPath(context); 
       CGContextAddPath(context, path); 
       CGContextDrawPath(context, kCGPathEOFill); 
       [self applyStrokePropertiesToContext:context atZoomScale:zoomScale]; 
       CGContextBeginPath(context); 
       CGContextAddPath(context, path); 
       CGContextSetAlpha(context,1.0); 
       CGContextStrokePath(context); 
      } 
      CGPathRelease(path); 
    } 
} 
} 

Je reçois fuite dans

CGPathRelease(interiorPath); 

et

return path; 

Je sais que je dois libérer chemin à l'aide CGPathRelease mais où pour le libérer tout je dois revenir.

Les deux fuites une énorme mémoire. J'ai travaillé dessus pendant des jours, aide svp.

Merci à l'avance

Répondre

7

Vous devez renommer votre méthode pour être -createPolyPath: de préciser qu'il revient un objet Core Foundation qui a besoin d'être libéré, puis dans le code que vous appelez -createPolyPath:, vous devez libérer comme ceci:

CGPathRef path = [someObjectOrClass createPolyPath:somePolygon]; 
// Do some stuff with the path 
CGPathRelease(path); 

Voir la "Memory Management Programming Guide for Core Foundation":

+0

Merci pour votre réponse Nick..I a travaillé là-dessus .. Mais encore il fuit .. – iPrabu

+0

Juste pour que vous Il est clair que vous devez ajouter le 'CGPathRelease()' après chaque point auquel vous appelez '-createPolyPath:', et pas seulement l'heure à laquelle vous l'appelez. –

+0

Désolé l'homme ne pouvait pas le faire correctement. J'ai donné CGPathRelease() après le -createPolyPath: appelé. Toujours des fuites. – iPrabu

1

Je pense que vous devez renommer votre méthode en commençant par new comme newPolyPath... . Je l'ai fait et cela fonctionne maintenant pour moi avec plus de fuites sur le chemin ...

Vous devez également utiliser CGPathRelease(path); après chaque utilisation de votre chemin.

+0

renommer la méthode n'affecte pas le fait qu'il y en ait ou non, cela n'affecte que ce que l'analyseur statique de Xcode considère comme une fuite. Vous devez appeler CGPathRelease chaque fois que vous appelez CGPathCreate ou vous aurez une fuite. –

1

Essayez d'utiliser CGPathRelease (chemin);

Par exemple:

CGMutablePathRef path = CGPathCreateMutable(); // created memory allocation 
CGPathCloseSubpath(path); 
CGPathRelease(path); // released path allocation 

Grande astuce trouvée ici:

http://the.ichibod.com/kiji/ios-memory-management-tips/