Nat! bio photo

Nat!

Senior Mull.

Twitter RSS

Github

NSArchiver and pointers ? Not anymore.

Another one of these not-so-precious WTF moments... I am probably just too late to catch up, but pointer values in archives are not possible anymore. And the exception message is kinda lacking...

#import <Foundation/Foundation.h>


@interface Foo : NSObject < NSCoding >
{
   id   referencedObject;
}

@end


@implementation Foo

- (void) encodeWithCoder:(NSCoder *) encoder
{
   [encoder encodeValueOfObjCType:@encode(void *) 
                               at:&referencedObject];
}

@end


int   main( int argc, char *argv[])
{
   @autoreleasepool
   {
       printf( "%s\n", @encode( void *));
      [NSArchiver archivedDataWithRootObject:[Foo new]];
   }
   return 0;
}

crashes on 32 bit and 64 bit Intel, because void * is now not a "known type" anymore ?

2015-06-18 12:00:35.878 NSArchiver[3746:50892] *** Terminating app due to uncaught exception 'NSArchiverArchiveInconsistency', reason: '*** type descriptor error for '^' in '^v': unknown type descriptor'

Memo

How to this better: The Archiver should have a method that is "setNonPersistable:". Whenever one is encoding a pointer, the encoding routine should be setting that flag. This flag would be part of the archive header.

That way an archive would know if it was safe to be persisted or not. That would keep archives trivially usable as an intra-process facility and you would have a clue, when archives get persisted/unpersisted, what has gone wrong.


Post a comment

All comments are held for moderation; basic HTML formatting accepted.

Name:
E-mail: (not published)
Website: