views:

709

answers:

2

I'm trying to add a placemark to a map. The placemark is built from an address completely outside of Address Book.

My placemark is appearing on the map, but when I try to pinch to zoom in I get a crash:

*** -[CALayer objectForKey:]: unrecognized selector sent to instance 0x4569dc0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[CALayer objectForKey:]: unrecognized selector sent to instance 0x4569dc0'

Here's how I'm setting up the address:

id theAddress = [NSDictionary dictionaryWithObjectsAndKeys:
                 [NSString stringWithFormat: @"%@ - %@", theAddress1 ? theAddress1 : @"", theAddress2 ? theAddress2 : @""], kABPersonAddressStreetKey,
                 theCity ? theCity : @"", kABPersonAddressCityKey,
                 theState ? theState : @"", kABPersonAddressStateKey,
                 theZip ? theZip : @"", kABPersonAddressZIPKey,
                 theCountry ? theCountry : @"", kABPersonAddressCountryKey,
                 nil];

I use the values in the address record to find the coordinate for the address (learned how to do this from this question & answer), then I add it to my map:

[mapView addAnnotation: [[[MKPlacemark alloc] initWithCoordinate: theCoordinate
                                               addressDictionary: theAddress] autorelease]];

The crash definitely seems to be caused by this MKPlacemark, as if I comment out the addAnnotation statement the code doesn't crash.

Any idea what's going on? I'm guessing I haven't got enough in the address record, but the error message is really unhelpful.

+1  A: 

You're over-releasing an NSDictionary object somewhere. Here's how you find it:

Under the Project menu, select 'Edit Active Executable'. In the Arguments tab, add an item to the "Variables to be set in the environment:" block with the name NSZombieEnabled and the value YES.

Happy Zombie Hunting.

John Franklin
Haven't found it yet, but I am indeed getting "message sent to deallocated instance." Thanks.
Steven Fisher
Huh. Instruments is showing that *MapKit* is releasing my dictionary repeatedly.
Steven Fisher
+1  A: 

Is something happening to that theAddress NSDictionary between when you create it and when you pass it to MKPlacemark's init method? I ask because it seems like you're crashing because your code is trying to treat a CALayer object like it's a collection class (that is, like it has an objectForKey: method), and the only collection class I see in there is that address dictionary.

John Biesnecker
Not before calling init, but it does look like something between me initing MKPlacemark and MKMapView using it.
Steven Fisher