Tuesday, 23 March 2010

KVO: observing self crashes Xcode


i picked programming cocoa again getting unexpected crash. xcode crashes on sometimes.
code part of catextlayer subclass. want observe when changes happen layer. app crashes when observing fontsize , foregroundcolor, not string , font. crash occurs in dealloc.
aren't supposed observe self self?
why layer being dealloc'ed?

code:
  -(id) init  {  	self = [super init];  	if (self)  	{  #if 1  		//observers  		for (nsstring *keypath in [nsarray arraywithobjects:@"string",@"font",@"fontsize",@"foregroundcolor", nil])  		{  			[self addobserver:self  				   forkeypath:keypath  					  options:nskeyvalueobservingoptionnew  					  context:null];  		}  #endif  	}  	return self;  }    -(void) dealloc  {  #if 0  #warning xcode crash!!!!!  	for (nsstring *keypath in [nsarray arraywithobjects:@"string",@"font",@"fontsize",@"foregroundcolor", nil])  	{  		[self removeobserver:self  				  forkeypath:keypath  					 context:null];  	}  #endif  	[super dealloc];  }  
this code crash it.
code:
  -(ibaction) test:(id)sender   {  	[mytextlayer setstring:[textfield stringvalue]]; //works ok  	[mytextlayer setfont:[nsfont fontwithname:@"verdana"   								size:48]];  //works ok    //	[mytextlayer setfontsize:11];   //crash  //	[mytextlayer setforegroundcolor:[[nscolor blackcolor] cgcolor]] ;//crash  }	  
 

look documentation of method removeobserver:forkeypath:context:

find?

i'd suggest learn how turn on warnings in code. appropriate build settings, code wouldn't have compiled. if saw warnings , ignored them, shame on you.
 


Forums Macs Mac Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment