EXC_BAD_ACCESS(code=2) on [[NSDateFormatter alloc] init] - objective-c

Since iOS 5.1 I'm getting EXC_BAD_ACCESS(code=2) errors when I instantiate an NSDateFormatter object.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // bad_access
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
Did anyone experience something similar or maybe even has a solution? It's making me crazy!
I'm using ARC for my project.
EDIT:
Even NSDateFormatter* dateFormatter = [NSDateFormatter new]; is giving me the same error.

I've had some similar problems some time ago. Ordinary 100%-error-free line causes this EXC_BAD_ACCESS, code=2, zombies show nothing.
The thing was that I had a death cycle, somehow calling function A caused calling function A again, that caused calling function A again and so on (at the stack trace there were a looooot of lines). So I've just ran out of memory and got EXC_BAD_ACCESS. Preventing code to enter that death loop solved that for me.
Hope this helps.

Related

UIDatePicker.date bug

I'm having a problem getting the date from UIDatePicker. I know the code of getting the date, but it just keeps on getting the CURRENT date, not the date form the picker.
- (IBAction)buttonPressed:(UIButton *)sender {
NSDateFormatter *formate = [[NSDateFormatter alloc]init];
NSDate *settedDate = self.myPickedDate.date;
[formate setDateFormat: #"dd.MMM.yyyy # HH:mm:ss"];
NSString *dateString = [formate stringFromDate:settedDate];
NSLog(#"datestring: %#", dateString);
}
I set myPicker to 15th May 2015 15:30 and Xcode logs out the current date (if it's 16:19 he will log out 22nd.Apr.2015 16:19, no matter what.
Xcode 5.1.1 on simulator iOS 7.1.2 (haven't tried on real device).
The problem is that in your viewDidLoad you are saying this:
self.myPickedDate = [[UIDatePicker alloc] init];
So, think about that code. What you are doing there is creating a new date picker, completely different from the one in your interface, and substituting it for the one in your interface, to which self.myPickedDate was previously set (because it is an outlet). So from now on, self.myPickedDate refers to a different date picker, one that is not in your interface (it is merely held in memory)! Therefore, nothing you do in the interface, such as setting the date in the date picker you see there, has any effect on self.myPickedDate.
Therefore, to solve the problem, delete that line of code.

Potential leak IPhone App

HarrisAnnotation *harrisAnnotation = [[HarrisAnnotation alloc] init];
[self.mapAnnotations insertObject:harrisAnnotation atIndex:0];
[harrisAnnotation release];
Running analyze on project show
Potential leak of an object
for
harrisAnnotation
Never mind, I'm not used to using Xcode. It was the line below.

Format of date when retrieving lastModified

I am comparing the date when a file was last modified for two files, one local and one on Amazon S3 server. I am using the AWS IOS SDK framework and can successfully request and receive response from the S3 server but I have trouble understanding the format of the returned s3 date.
On my local machine the date format for lastModified is "2011-07-21 18:43:15 -0400" while for the file residing on the S3 server it is "2011-10-15T16:25:49.000Z".
My local info is obtained using:
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *attr = [fm attributesOfItemAtPath:filePath error:nil];
NSDate *localDate = [attr objectForKey:NSFileModificationDate];
while my S3 info is obtained using
for (S3ObjectSummary *object in [listObjectsResult objectSummaries]) {
NSDate *s3date = [object lastModified];
}
Does anyone know if I can convert the date for the S3 file to a format that I can use to compare these two dates using:
NSTimeInterval deltaSeconds = [s3Date timeIntervalSinceDate: localDate];
or am I doing something wrong here? Right now my program crashes with
[NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x200351360.
probably because the s3 date format is not in proper format. I am quite new to using the AWS S3 SDK so all help is greatly appreciated. If anyone also knows of some good tutorials for this framework (apart from the demo code that comes with it), that would be great. Cheers, Trond
It looks to me as [object lastModified] simply returns a NSString and not an NSDate object, as stated in the documentation.
NSDateFormatter can be used in this case to create a NSDate object from the string:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'mm':'ss.SSS'Z'"];
NSDate *s3date = [dateFormatter dateFromString:[object lastModified]];
[dateFormatter release];
The Date Formatting guide has lots of handy examples. You may need to tweak the format string slightly as i have not tested it.
Had some trouble with the given answer - seems that there is some sort of duplicated 'ss'+'mm' stuff in there that prevents the string from correctly translating. Additionally, because the time zone in the string is given as UTC, you have to set the time zone for the formatter. Here is the code I used (that worked for me), I turned it into a category, and default to using the previously provided string in the event the one that works for me doesn't work for you:
#interface S3ObjectSummary (lastModifiedDate)
- (NSDate *)lastModifiedDate;
#end
#implementation S3ObjectSummary (lastModifiedDate)
- (NSDate *)lastModifiedDate
{
NSString *modifiedString = [self lastModified];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // Note, this is ARC, add autorelease if you aren't ARC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];
NSDate *modifiedDate = [dateFormatter dateFromString:modifiedString];
if(!modifiedDate) // Try other format in case of fail http://stackoverflow.com/a/7799607/285694
{
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'mm':'ss.SSS'Z'"];
modifiedDate = [dateFormatter dateFromString:modifiedString];
}
return modifiedDate;
}
#end
Looks like this also works:
NSDate *summaryDate = [NSDate dateWithISO8061Format:summary.lastModified];

ALAssetPropertyDate returns "wrong" date

I'm currently working on a project in which i need to read some (Latitude, Longitude and date ) EXIF data. The location data seems correct, but the date i'm getting seems to be the "date last modified" date.
{
CLLocation *loc = [asset valueForProperty:ALAssetPropertyLocation];
NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
//Returns Last modified date(Picture was taken ... let's say september
//last year and it would return the date and time I 'modified' the image).
NSString *latitude = [NSString stringWithFormat:#"%g",loc.coordinate.latitude];//Returns correct Latitude
NSString *longitude = [NSString stringWithFormat:#"%g",loc.coordinate.longitude];//Returns correct Longitude
}
My question is: Am i doing something terribly wrong, or is this expected behavior.
I also tried to use the loc.timestamp instead of the [asset valueForProperty:ALAssetPropertyDate] but these returned the same date.
Any help is greatly appreciated !
You can also get Exif DateTimeOriginal through ALAsset.
NSDateFormatter *dateFormatter = [[NSDateFormatter new] autorelease];
dateFormatter.dateFormat = #"y:MM:dd HH:mm:ss";
NSDate *date = [dateFormatter dateFromString:[[[[asset defaultRepresentation] metadata] objectForKey:#"{Exif}"] objectForKey:#"DateTimeOriginal"]];
Getting metadata from asset requires to load Exif header on memory (or entire image file?) and those methods above seems to use autorelease pool for the memory spaces. This may cause memory shortage or worse crash if you do a batch process for thousands of images.
To work around for memory shortage, you may use Ad-Hoc autorelease pool.
NSDateFormatter *dateFormatter = [[NSDateFormatter new] autorelease];
dateFormatter.dateFormat = #"y:MM:dd HH:mm:ss";
for (ALAsset *asset in thousandsOfAssets) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSDate *date = [dateFormatter dateFromString:[[[[asset defaultRepresentation] metadata] objectForKey:#"{Exif}"] objectForKey:#"DateTimeOriginal"]];
// do something
[pool release];
}
EDIT: correct wrong dateFormat (SS -> ss). Thanks #code-roadie
Though it's not explicitly documented, I'm guessing that this is the expected behavior. The date refers to when the asset was created and when you're modifying the image, you're probably implicitly creating a new asset. Nothing in the ALAsset documentation suggests that its properties correspond to the image's EXIF data.
To access the EXIF data, you could use the Image I/O framework (available since iOS 4.0), specifically the CGImageSourceCopyProperties function.

Problem with memory management

Here is a sample code, i am trying to import contacts for the iphone to my app.
-(IBAction)import_Clicked:(id)sender{
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; //leaking here
picker.peoplePickerDelegate = self;
// Display only a person's phone, email, and birthdate
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
picker.displayedProperties = displayedItems;
[self presentModalViewController:picker animated:YES];
[picker release];}
i ran this on instruments and it shows me 100% leak at line where i alloc abpeoplepickernavigationcontroller. i realsed it after persentmodalviewcontroller. where else could i go wrong.
Any Help , Please.....
There seems to be a strange SDK bug at large here... Have a read of the official Apple dev forums here for more information and a solution.
Strange, that doesn't looks like a leak to me, heard that Instruments (rarely) report false leaks.
EDIT: forget what follows and read bbum comment instead :)
Could you please try removing [picker release] and then use autorelease instead :
BPeoplePickerNavigationController *picker = [[[ABPeoplePickerNavigationController alloc] init] autorelease];
Then see if instruments still report a leak? If not, keep your original code and ignore that false alert...
That is almost the same but using NSAutoReleasePool might change Instruments behavior.
Please also note that explicitly releasing like you did is a more cleaner approach than autoreleasing.