3

I have a problem concerning memory warnings on the iPhone. I remove overlays and insert new ones. This happens every 5 seconds. But after a while, like half a minute, I get a memory warning and my app crashes.

What could I do, the subview of my overlay is the problem...
The triangle will be created with "drawRect".
I tested it, without the triangle it would be no problem and the app runs stable.

But only when I add the triangle to the circleView.

When the triangle class is empty, there is the same problem.

- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{       
    MKOverlayView *overlayReturn = nil;

    if ([overlay isKindOfClass:[MKCircle class]] == YES) {
    MKCircleView *circleView = [[[MKCircleView alloc] initWithOverlay:overlay]autorelease] ; 

    circleView.strokeColor = [UIColor redColor]; 
    circleView.lineWidth = 1;
    circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.4];

   Triangle* triangle = [[Triangle alloc]initWithFrame:CGRectMake(circleView.circle.radius*10-1000, circleView.circle.radius*10-1000, 2000, 2000)];

    triangle.backgroundColor = [UIColor clearColor];
    [circleView addSubview:triangle];
    [triangle release];

    return circleView;
}
Simon
  • 31
  • 2

3 Answers3

1

I've had almost the same problem. It just seems that the kit is not handling multiple overlays that well. It doesn't matter if it is an MKCircle, MKPolygon or custom...

The problem is that viewForOverlay isn't reusing your overlays. Therefore creating a new overlay AND triangle each time.

There is a workaround described in the answer to this question. It is on the Apple Developer forums though: get it here...

This way you'll create one overlay with all your overlays in it. Resulting in far less memory usage.

Community
  • 1
  • 1
wkberg
  • 391
  • 2
  • 12
  • multiple overlays are fine, the missing reusing you mention is the problem IMO – Daij-Djan Jul 06 '13 at 10:40
  • Indeed multiple overlays are fine. But NOT reusing them causes the problems. Each overlay is created as a new layer on the map. Therefore very memory-heavy... The work-around creates one overlay from all your overlays and therefore no need for reuse because you only use one. – wkberg Jul 06 '13 at 11:15
  • You simply can't. Not in the way you posted. Overlays aren't created that way and don't use a `dequeueReusable...` The work-around that I posted creates one OverlayView from all your Overlays. Therefore there is no need to reuse because you use only one! – wkberg Jul 06 '13 at 11:24
  • No problem at all! ;-) It would be a lot easier though if it worked that way! :D – wkberg Jul 06 '13 at 11:59
1

Start with Instrument's Leaks and Allocations to see what's taking up your memory. You're probably leaking something inside the Triangle class if I had to guess.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • No.. if the class is empty, there are the same problem – Simon Jun 17 '11 at 17:31
  • Still start with Instrument's Leaks and Allocations. This will tell you what kind of object is consuming memory. – Rob Napier Jun 17 '11 at 17:45
  • 1
    Yes, but what object is consuming memory? Allocations will tell you that. A "leak" and a "holding onto memory you shouldn't be" aren't the same thing, but they both eat memory. – Rob Napier Jun 17 '11 at 19:07
0

I'm not sure how you're using that method (in a loop, or what), but, if you can, set up another NSAutoreleasepool to release those objects as soon as you can.

Here's an example of how you could use it with a loop to get rid of autoreleased objects ASAP.

for (NSInteger i = 0; i < 99999999999999; i++ )
{
    NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];
    NSString *string = [NSString string];
    // code
    [innerPool release];
}
thomashw
  • 956
  • 4
  • 7