Sqlite - "incomprehensible archive" problem - objective-c

I am working on a iphone/Ipod touch project involving saving and fetching data from an sqlite database. It is working fine, but at some points in development i get this error (when the app starts):
erminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x4e, 0x49, 0x42, 0x41, 0x72, 0x63, 0x68, 0x69)'
2009-11-09 10:08:28.810 FagNavigasjon[1627:20b]
Previously i have only been able to solve this problem by reverting to earlier point in develepment and continuing. This time i have found out that if i comment out a certain part of the code, the app doesnt crash.
while(sqlite3_step(selectstmt3)==SQLITE_ROW){
/*
NSString *navn=[[NSString alloc] initWithUTF8String: (char *)sqlite3_column_text(selectstmt3, 0)];
NSString *telefon=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(selectstmt3, 1)];
NSString *fodselsar=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(selectstmt3, 2)];
NSString *ariframtiden=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(selectstmt3, 3)];
NSString *verneplikt=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(selectstmt3, 4)];
NSString *utdannelse=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(selectstmt3, 5)];
NSString *epost=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(selectstmt3, 6)];
appDelegate.navn=navn;
appDelegate.epost=epost;
appDelegate.telefon=telefon;
appDelegate.fodselsar=fodselsar;
appDelegate.studiear=ariframtiden;
appDelegate.utdannelse=utdannelse;
appDelegate.verneplikt=verneplikt;
*/
}
But of course the app doesnt load the data anymore. This code previously worked, and i have not changed it prior to this error. I have, however, added new records to the database, but these seem okay when i review them in SQLite manager. Can anyone tell me what can cause this problem? I am thankful for any responses!

Have you tried uncommenting every line one by one to see which line is causing the problem? If you know which line is causing it try to see if the data in the database is correct, also if you are using version control try reverting your database back to before you added the new records.
p.s.
Have you looked at Core Data? It could make your life a lot easier.

Just for the records: i have resolved this issue, though i still dont really understand how. As I knew which table caused the problem. I went in there using SQLlite manager and changed the values om some varchar fields, and the problem went away... The old values looked ok, though. Wish i could explain why this solved it...

Related

UIDocumentInteractionController weird behaviour on iphone with iOS7

I am hitting the wall with a problem for two days now and i would like your help. Before i start i should say that this problem is on iPhone 5 with iOS7 (I have also tested on iPhone 4 with iOS 6 and iPad 2 with iOS 7). This problem began when i tried to upgrade an application that has been on AppStore (iOS4 initially) and tried to make it iOS 7 compatible (supported on iOS6 onwards).
The scenario is pretty simple. I have a view with is a UIDocumentInteractionControllerDelegate. I download a file from a web service save it on the NSTemporaryDirectory and allow the user to Preview or Open In another app using the presentOptionsMenuFromRect. The code simplified is as follow:
I have declared a #property (nonatomic, strong) UIDocumentInteractionController *docController;
#autoreleasepool {
NSString *fileName = "uniquefilename"
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
fileURL = [NSURL fileURLWithPath:filePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:filePath]){
NSData *fileData = [NSData dataWithContentsOfURL:[NSURL URLWithString:"theurlofthefile"]];
NSError *writeError = nil;
[fileData writeToURL: fileURL options:0 error:&writeError];
if(writeError) {
//show error
}
}
docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
if (isIpad) {
[docController presentOptionsMenuFromRect:CGRectMake(location.x + 400,location.y, 100, 100) inView:tableView animated:YES];
}
else{
[docController presentOptionsMenuFromRect:CGRectZero inView:self.tabBarController.view animated:YES];
}
}
The problem is that i receive all kind of errors, i repeat the same process all the time and i get different errors, sometimes it works for many times in a row, sometimes it fails from the first go. The errors i receive amongst others which i will add when i get them again are:
* Terminating app due to uncaught exception 'NSGenericException', reason: '* Collection <__NSSetM: 0x16ff61e0> was mutated while
being enumerated.'
malloc: * error for object 0x177a56a4: incorrect checksum for
freed object - object was probably modified after being freed.
malloc: * error for object 0x1c2c8b50: double free * set a
breakpoint in malloc_error_break to debug
When the OptionsMenu is showed successfully i see "AirDrop: collectionView:layout:insetForSectionAtIndex:, orientation: 1, sectionInset: {0, 10, 0, 10}" in the console.
I tried enabling NSZombies and putting a breakpoint for malloc error but did not help me in any way.
Please help me or guide me to the right direction.
Thank you.
Bit late to answer, but hopefully it will help other people.
I had exactly the same issue when saving a file and presenting a UIDocumentInteractionController, it was absolutely random sometimes it would would perfectly 10 times in a row and sometimes it would crash on first try.
It seems to be caused by the file not having finished writing to disk, what fixed it for me was adding a delay before presenting the UIDocumentInteractionController to ensure that the file has finished writing to disk

writeToFile fails when writing to desktop

I have this code snippet below. The purpose is to write the contents of an array to a plist file. When I run this I don´t get any errors and everything seems to be fine. The problem is that the file doesn´t appear on the desktop. I used writeToFile before in other projects without any problems. What is wrong with it? Greetings from Switzerland, Ronald Hofmann.
- (IBAction) doActionPlist: (NSButton *) sender
{
NSString *fileExt = #".plist";
NSArray *myResultArray = [NSArray arrayWithObjects:
#"Product Number Unlimited Desktop:",
#"4DDP120-UUS001-ANLKR-17C6B",
#"4DDP130-UUS001-ANQEI-1BC12",
#"4USE120-UUS001-80BY1-1EAB0",
#"4USE130-UUS001-6NTUE-11D64",
#"qA0iHnw4EIgOALgAAAQCmUtj",
#"qA0iHnw6EIgAALgAAPUBhByg",
#"4DDP11U00USN001",
#"4DDP13U12USN001",
#"4UCL00U00USN001",
#"4UCL00U00USN010",
#"4UCL11U00USN001",
#"4UCL11U00USN010",
#"4UCL12U11USN001",
#"4UCL12U11USN010",
#"4UCL13U12USN001",
#"4UCL13U12USN010",
#"4USE12U11USN002",
#"4USE13U12USN002",
#"4UWE13U12USN999",
nil];
NSString *thePath = [NSString stringWithFormat:#"/Users/ronny/Desktop/Testfile%#",fileExt] ;
[myResultArray writeToFile:thePath atomically:YES] ;
}
Unfortunately, it can be difficult to debug this call because it only returns YES or NO with no further information about why it succeeded or failed. The fact that it's not throwing any exceptions doesn't necessarily mean that nothing is going wrong; it just means that NSArray can't be bothered to tell you about it.
If you're really at wit's end, you might try converting your NSArray to NSData using NSPropertyListSerialization and then using writeToFile:options:error instead. If you do this, post the error message and someone should be able to offer better debugging advice.

Simple CoreData example throwing exception

I'm going a little mad I think. This is the VERY first thing I do and I get the crash (seen below). The crash is the moment I try to step over the executeFetchRequest.
If I remove request.sortDescriptors= sortDescriptors it works.
I must be doing something insanely stupid. But even after walking away a couple times, I still can't see it. index is a non-optional Integer32, with default of 1.
- (void)awakeFromNib {
[self defaultData];
}
- (void)defaultData {
NSManagedObjectContext* moc= self.managedObjectContext; // all code with this
[NSEntityDescription insertNewObjectForEntityForName:#"Envelope"
inManagedObjectContext:moc];
NSFetchRequest* request = [NSFetchRequest
fetchRequestWithEntityName:#"Envelope"];
NSArray* sortDescriptors= [NSArray arrayWithObject:#"index"];
request.sortDescriptors= sortDescriptors;
NSError* error = nil;
NSArray* results = [moc executeFetchRequest:request error:&error];
}
And here's my crash:
-[__NSCFConstantString key]: unrecognized selector sent to instance 0x240f4
Tried deleting the 'iPhone Simulator' folder. Tried deleting and recreating the xcdatamodeld. Perhaps a leak in my nib files (though can't see anything). Tried gcc (currently Apple LLVM 3.1). I've tried moving everything over to a new project. I've tried starting a project completely fresh. I've tried saving the context before doing the fetch.
I already know I'm going to hate the answer. It's going to be stupid, I'm going to feel foolish, but I need another set of eyes.
Thanks
Greg
sortDescriptors is supposed to be an array of NSSortDescriptor; your sortDescriptors just has #"index" (an NSString). Try this:
NSArray* sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"index" ascending:NO]];
request.sortDescriptors = sortDescriptors;

AVAsset has no tracks or duration when created from an ALAsset URL

I'm pulling all of the video assets from ALAssetsLibrary (Basically everything that's being recorded from the native camera app). I am then running an enumeration on each video asset that does this to each video:
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
//Get the URL location of the video
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
NSURL *url = [representation url];
//Create an AVAsset from the given URL
NSDictionary *asset_options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVAsset *avAsset = [[AVURLAsset alloc] initWithURL:url options:asset_options];//[AVURLAsset URLAssetWithURL:url options:asset_options];
//Here is the problem
NSLog([NSString stringWithFormat:#"%i", [avAsset.tracks count]]);
NSLog([NSString stringWithFormat:#"%f", CMTimeGetSeconds(avAsset.duration)]);
}
NSLog is reporting that the AVAsset that I've gotten from my ALAsset has 0 tracks, and has a duration of 0.0 seconds. I checked the url, and it's "assets-library://asset/asset.MOV?id=9F482CF8-B4F6-40C2-A687-0D05F5F25529&ext=MOV" which seems correct. I know alAsset is actually a video, and the correct video, because I've displayed alAsset.thumbnail, and it's shown the correct thumbnail for the video.
All this leads me to believe there's something going wrong in the initialization for avAsset, but for the life of me, I can't figure out what's going wrong. Can anyone help me?
Update:
I think i've confirmed that the url being given to me by ALAssetRepresentation is faulty, which is weird because it gives me the correct thumbnail. I added this code in:
NSLog([NSString stringWithFormat:#"%i", [url checkResourceIsReachableAndReturnError:&error]]);
NSLog([NSString stringWithFormat:#"%#", error]);
It gives me this:
0
Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x19df60 {}
I'm still not sure what would cause that. The only thing I'm noticing is the url, which is "assets-library://asset/asset.MOV?id=9F482CF8-B4F6-40C2-A687-0D05F5F25529&ext=MOV" is different from what I've seen as I've been searching around for this. The one i've seen elsewhere looks more like "assets-library://asset/asset.MOV?id=1000000394&ext=MOV", with a number instead of an alphanumeric, dash separated name.
If it helps, I'm using XCode 4.2 Beta, and iOS5. Please let me know if you can think of anything. Thanks.
Okay, looks like it was a bug in the iOS5 beta v1. I upgraded to the newest and it worked. Thanks to those who took a look at my question.

Objc memory crash with autorelase

I have been hunting all over my code and can't find the source of this crash:
I am trying to decode an object with an NSKeyedUnarchiver and it crashes on it every time and says:
*** __NSAutoreleaseFreedObject(): release of previously deallocated object (0x1008ad200) ignored
*** __NSAutoreleaseFreedObject(): release of previously deallocated object (0x1008ab200) ignored
*** __NSAutoreleaseFreedObject(): release of previously deallocated object (0x1008a8c00) ignored
Ha my bad the reason why initWithCoder was not getting called was it was having issues with the [super initWithCoder:]; This is still driving me crazy. I looked and the pointers and the NSData objects are what are going wrong:
vertices = malloc(size_point3D * vertexCount);
textureCoords = malloc(size_point2D * textureCount);
normals = malloc(size_point3D * normalCount);
faces = malloc(sizeof(GLuint) * faceCount);
NSData *vertexData = [[NSData alloc] initWithData:[coder decodeObjectForKey:#"vertices"]];
NSData *textureData = [[NSData alloc] initWithData:[coder decodeObjectForKey:#"textureCoords"]];
NSData *normalData = [[NSData alloc] initWithData:[coder decodeObjectForKey:#"normals"]];
NSData *faceData = [[NSData alloc] initWithData:[coder decodeObjectForKey:#"faces"]];
memcpy(vertices, [vertexData bytes], sizeof(point3D) * vertexCount);
memcpy(textureCoords, [textureData bytes], sizeof(point2D) * textureCount);
memcpy(normals, [normalData bytes], sizeof(point3D) * normalCount);
memcpy(faces, [faceData bytes], sizeof(GLuint) * faceCount);
[vertexData release];
[textureData release];
[normalData release];
[faceData release];
I have tried retaining everything in this part (even the string) but it does not help.
This was a hard one to solve partly because debugging memory behaves inconsistently.
I have 2 classes JGStaticModel and JGModel. For some reason, the unarchiver would pick one of those at random so sometimes initWithCoder was sent to JGModel and not JGStaticModel. This led me to think it was not being called. Also since their structure is slightly different it had issues and crashed. The reason why I got the autorelease problem was I patched some memory problems in JGStaticModel but not JGModel so it would crash on the memory because I had not fixed it there.
Thanks for all the help!
Try turning on NSZombieEnabled, this should help you track down the problem.
If neither tools (Run -> Run with Performance tools) and NSZombiesEnabled helps, you can override - (id)retain and - (void)release methods of the class that caused the exception. Call super implementation and log retain/release. You can break in this methods to see the call stack. This way isn't beautiful, however, it helped me few time to figure out where was an extra release/autorelease call
This problem is very easy to solve with the solution given here.
The relevant part is:
If an environment variable named "NSAutoreleaseHaltOnFreedObject" is set with string value "YES", the function will automatically break in the debugger