0

I'm experiencing this crash in my application and I can't seem to figure out what is going wrong.

I've got an object with a property that holds on to a Class struct. I declare it like this:

@property (nonatomic, assign) Class myClass; 

Now, I'd like to serialize this object, so implemented NSCoding. That looks like this:

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super init];
    self.myClass = NSClassFromString([coder decodeObjectForKey:@"myClass"]);
    self.title = [coder decodeObjectForKey:@"title"];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:NSStringFromClass(self.myClass) forKey:@"myClass"];
    [coder encodeObject:self.title forKey:@"title"];
}

However, when I execute this line

    [coder encodeObject:NSStringFromClass(self.myClass) forKey:@"myClass"];

I get an EXC_BAD_ACCESS crash and I am struggling to find out why. Any ideas?

Thanks!

Sean Danzeiser
  • 9,141
  • 12
  • 52
  • 90
  • Try logging `self.controllerClass`. What is it? –  Jul 14 '13 at 22:20
  • 1
    Why is the property "assign" and not "strong" ? And try to proceed in steps: NSString * myClassString = NSStringFromClass(...) ; [coder encodeObject: myClassString ...]... Is there anything that jumps at you now? – verec Jul 14 '13 at 22:22
  • 1
    Please check the retain count of myClass using Instruments. If you change myClass from assing to retain that will fix the problem (alredy mentioed by @verec). – AAV Jul 14 '13 at 22:29
  • @H2CO3 logging gives me a crash. – Sean Danzeiser Jul 14 '13 at 22:32
  • retaining worked... i mistakenly thought I was using ARC in this particular library – Sean Danzeiser Jul 14 '13 at 22:37
  • @verec: Class objects should live forever. Retaining and releasing class objects should have no effect. – newacct Jul 16 '13 at 01:30
  • @AmitVyawahare: Why? Class objects should not be deallocatable. – newacct Jul 16 '13 at 01:31

0 Answers0