1

I have a PlaceAnnotation class that I fill into an NSMutableArray. In viewDidLoad, i initiate ihatethis

_ihatethis = [[NSMutableArray alloc]init];

I use a MKLocalSearchCompletionHandler to search. And handle the mapitems like this:

for (MKMapItem *mapItem in [response mapItems]){

     PlaceAnnotation *place = [[PlaceAnnotation alloc] init];
     [place assignTitle:[[mapItem placemark] name];

     [_ihatethis addObject:place];
}

[_ihatethis removeObjectAtIndex:2]; /*BAD ACCESS HERE*/
[_tableView reloadData];

This is my PlaceAnnotation.h file

@interface PlaceAnnotation : CLPlacemark <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic) NSDictionary* dict;
//@property (nonatomic) NSURL *url;
@property (nonatomic) NSString *phoneNum;
@property (readonly) BOOL selected;

-(void)assignTitle:(NSString *)newTitle;
-(void)assignSelected:(BOOL) boolVal;

This is my PlaceAnnotation.m file

#import "PlaceAnnotation.h"

@interface PlaceAnnotation ()

@property (readwrite) NSString *title;
@property (readwrite) BOOL selected;

@end

@implementation PlaceAnnotation

-(void) assignTitle:(NSString *)newTitle {
    if ( ![newTitle isEqualToString:[self title]]){
        self.title = newTitle;
    }
}

-(void) assignSelected:(BOOL)boolVal{
    self.selected = boolVal;
}

@end


@end

This is my first post, I have read a ton of answers that responded to exc_bad_access questions and I cannot figure this out. So I think that somehow the placeannotations are getting forgotten and released. So when i go to delete is later it is gone. I really am confused and angry.

  • 1
    How's `_ihatethis` declared? And b.t.w. it should be `_iHateThis`. In Objective-C we're quite strict about style. – Nikolai Ruhe Nov 25 '13 at 22:13
  • 1
    Are you using ARC or manual retain count? – Nikolai Ruhe Nov 25 '13 at 22:14
  • Is the declaration `_ihatethis` being initialized before running through your loop? Second: does your loop have at least 3 objects being added? (`[_ihatethis removeObjectAtIndex:2]` is removing the third object in your array). – Matt Nov 25 '13 at 22:15
  • You showed how `_ihatethis`is being initialized, but not how it is declared (e.g. `NSMutableArray *_ihatethis` or `NSArray *_ihatethis`). If it's declared as the latter, you're going to have a bad time. – Matt Nov 25 '13 at 22:24
  • @Matt Declaring the array `NSArray` would trigger compiler warnings, not a mach (runtime) exception. – Nikolai Ruhe Nov 25 '13 at 22:33
  • What about the differences between the property `title` in the interface versus the interface extension? Is copying still occurring when calling the method `-assignTitle:`? Or is the `title` creating a strong reference to `newTitle`? – Matt Nov 25 '13 at 22:38
  • This has got nothing to do with Xcode. – dreamlax Nov 25 '13 at 23:13
  • Sorry, i switch it after getting frustrated. it is a property of the view, declared " @property NSMutableArray *ihatethis. I am using ARC. dreamlax, i dont know if it is some thing wrong with xcode 5. – William Franklin Katz Nov 25 '13 at 23:14
  • Matt, the title is readonly, because that is how it is in CLPlacemark – William Franklin Katz Nov 25 '13 at 23:16
  • @dreamlax , you're right. I thought it could be something with xcode 5 because this worked in xcode 4. But it ended up being a CLPlacemark issue so I updated the title. Thanks! – William Franklin Katz Dec 02 '13 at 19:59

3 Answers3

1

If the crash really is in this line [_ihatethis removeObjectAtIndex:2]; and it's a EXC_BAD_ACCESS then there are two possibilities:

  1. _ihatethis is pointing to a broken array (unlikely, if the loop went through).
  2. An object in the array has been overreleased or has broken memory management in its dealloc method.
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • I am using ARC and the only objects i am using are PlaceAnnotation, a class i made to extend CLPlacemark. Do you have any ideas of where I should look for this broken memory? – William Franklin Katz Nov 25 '13 at 22:48
0

It may be that you're trying to remove an item at an index greater than the array's count. Try to output the count just before [_ihatethis removeObjectAtIndex:2];, like this:

`
NSLog(@"count: %d", [_ihatethis count]);
[_ihatethis removeObjectAtIndex:2]; /*BAD ACCESS HERE*/
`

If the count is smaller than 3, then you're trying to remove an object at an index outside the array's bounds.

  • Out of bounds access would not result in an EXC_BAD_ACCESS but an Objective-C exception with a proper error message. – Nikolai Ruhe Nov 25 '13 at 22:20
0

I changed this

@interface PlaceAnnotation : CLPlacemark <MKAnnotation>

to this

@interface PlaceAnnotation : NSObject <MKAnnotation>

so i guess the problem was with the dealloc of CLPlacemark. Thanks

  • It would appear the CLPlacemark is the problem. I found that simply new'ing CLPlacemark will cause an EXC_BAD_ACCESS at the end of the method with ARC. I haven't been able to figure out why, but it sure makes it tough extend CLPlacemark and then test them. – David Potter May 16 '14 at 19:08