title: iOS removing MKMapView overlays not working
body: <p>I would like to remove all the overlays of my map at one point, and I tried different ways but it never works.</p>
  
  <p>Last try I did <code>[self.mapView removeOverlays:self.mapView.overlays];</code> and it still doesn't work. Any idea how I can remove those overlays?</p>
  
  <p>Thank you.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I have the error: <code>malloc: *** error for object 0x5adc0c0: pointer being freed was not allocated
  *** set a breakpoint in malloc_error_break to debug</code></p>
  
  <p>I think I know why, but don't really know how to fix it...
  Here is the code when I need to draw another line on my mapView:</p>
  
  <pre><code>// Create a c array of points. 
  MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D) * 2);
  
  // Create 2 points.
  MKMapPoint startPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(oldLatitude, oldLongitude));
  MKMapPoint endPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(newLatitude, newLongitude));
  
  // Fill the array.
  pointsArray[0] = startPoint;
  pointsArray[1] = endPoint;
  
  // Erase polyline and polyline view if not nil.
  if (self.routeLine != nil) {
      [_routeLine release];
      self.routeLine = nil;
  }
  
  if (self.routeLineView != nil) {
      [_routeLineView release];
      self.routeLineView = nil;
  }
  
  // Create the polyline based on the array of points.
  self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
  
  // Add overlay to map.
  [self.mapView addOverlay:self.routeLine];
  
  // clear the memory allocated earlier for the points.
  free(pointsArray);
  
  // Save old coordinates.
  oldLatitude = newLatitude;
  oldLongitude = newLongitude;
  </code></pre>
  
  <p>So I release the <code>routeLine</code> object which is the previous overlay. So when I tried to remove it, it crashes because it has already been deallocated.</p>
  
  <p>Here is the code for the mapView delegate for adding overlay views:</p>
  
  <pre><code>- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id &lt;MKOverlay&gt;)overlay
  {
      MKOverlayView* overlayView = nil;
  
      if(overlay == _routeLine) {
          // If we have not yet created an overlay view for this overlay, create it now. 
          if(self.routeLineView == nil) {
              self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:_routeLine] autorelease];
  
              self.routeLineView.fillColor = [UIColor blueColor];
              self.routeLineView.strokeColor = [UIColor blueColor];
  
              // Size of the trace.
              self.routeLineView.lineWidth = routeLineWidth;
          }
  
          overlayView = self.routeLineView;
      }
  
      return overlayView;
  }
  </code></pre>
  
  <p>If you guys know how I can fix this issue, removing all overlays from my MKMapView it would be awesome!</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I tried not releasing my <code>routeLine</code> and <code>routeLineView</code> objects and it works now. It seems like there are no leaks too. So now I'm doing this:</p>
  
  <pre><code>// Erase polyline and polyline view if not nil.
  if (self.routeLine != nil) {
      //[_routeLine release];
      self.routeLine = nil;
  }
  
  if (self.routeLineView != nil) {
      //[_routeLineView release];
      self.routeLineView = nil;
  }
  </code></pre>