malloc - __NSCFString appendString: crash for NSMutableString in Cocoa -


i have been facing issue , admit lack of fundamental concepts of memory managements. i've not been able solve , trust me, i've been trying many things out.

in app, there 4 threads (can 12 threads in future) read rs232 ports. in each thread (threadrs232read), i'll append rs232 characters respective nsmutablestring using appendstring. nsmutablestring grow incredibly large appendstring until full test completed. , periodically inside test, clearrdbuffstr called clear string. app crashes in appendstring. if lucky, can run few tests normally, crashes on first run. below code snippets , crash log.

appcontroller.h ... `@interface appcontroller : nsobject { ... nsmutablestring *buffstr1, *buffstr2, *buffstr3, *buffstr4; ...}  appcontroller.m ... //in -(id)init buffstr1 = [[nsmutablestring alloc] initwithstring:@""]; buffstr2 = [[nsmutablestring alloc] initwithstring:@""]; buffstr3 = [[nsmutablestring alloc] initwithstring:@""]; buffstr4 = [[nsmutablestring alloc] initwithstring:@""]; ... // in -(void)dealloc [buffstr1 release]; [buffstr2 release]; [buffstr3 release]; [buffstr4 release];` 

in file rs232rw.m, thread used update buffstr1 4

rs232rw.m - (void)threadrs232read:(id)argument {  nsautoreleasepool *pool = [[nsautoreleasepool alloc] init]; .... //read rs232 port buffer , etc... nsstring *buffstr = [nsstring stringwithutf8string:buff];     switch (portno) {         case 0:             if (buffstr != nil)             {                 [buffstr1 appendstring:buffstr];             }              break;         case 1:             if (buffstr != nil)             {                 [buffstr2 appendstring:buffstr];             }             break;         case 2:             if (buffstr != nil)             {                 [buffstr3 appendstring:buffstr];             }              break;         case 3:             if (buffstr != nil)             {                 [buffstr4 appendstring:buffstr];             }             break;         case 4:             if (buffstr != nil)             {                 [buffstr5 appendstring:buffstr];              }             break;          ....  // clearrdbuffstr called other part of program clear buffer. -(void) clearrdbuffstr:(int) portno { switch (portno) {     case 0:         [buffstr1 setstring:@""];         break;     case 1:         [buffstr2 setstring:@""];         break;     case 2:         [buffstr3 setstring:@""];         break;     case 3:         [buffstr4 setstring:@""];         break;     .... 

the app crashes @ 1 of appendstring above.

the crash log below:

   ....   crashed thread:  3  exception type:  exc_crash (sigabrt) exception codes: 0x0000000000000000, 0x0000000000000000  application specific information: objc[584]: garbage collection off *** error object 0x6d02b600: double free .... thread 3 crashed: 0   libsystem_kernel.dylib          0x905819c6 __pthread_kill + 10 1   libsystem_c.dylib               0x926ecf78 pthread_kill + 106 2   libsystem_c.dylib               0x926ddbdd abort + 167 3   libsystem_c.dylib               0x92701508 szone_error + 333 4   libsystem_c.dylib               0x92702dd1 free_small_botch + 102 5   com.apple.corefoundation        0x97ee51e8 __cfallocatorsystemdeallocate + 24 6   com.apple.corefoundation        0x97ee51ba cfallocatordeallocate + 266  7   com.apple.corefoundation        0x97ee50a2 __cfstrdeallocatemutablecontents + 178 8   com.apple.corefoundation        0x97ee422b __cfstringchangesizemultiple + 3147 9   com.apple.corefoundation        0x97f86010 __cfstringcheckandreplace + 496 10  com.apple.corefoundation        0x97f95dad -[__nscfstring appendstring:] + 45 11  com.toptestdfu                  0x00109f6d -[appcontroller(rs232rw)  threadrs232read:] + 752 12  com.apple.foundation            0x92aabf7d -[nsthread main] + 45  13  com.apple.foundation            0x92aabf2d __nsthread__main__ + 1582 14  libsystem_c.dylib               0x926eaed9 _pthread_start + 335 15  libsystem_c.dylib               0x926ee6de thread_start + 34 

the cocoa mutable objects such nsmutablestring not thread safe. it's arrange synchronisation using form of lock.

check out nslock 1 option.

you might try making 4 nsmutablestrings atomic properties rather ivars. might work in case, although atomic doesn't guarantee thread safety.

[edit] ... actually, if in case, sure each thread using different port number, it's not nsmutablestrings such problem. it's nsstring *buffstr = [nsstring stringwithutf8string:buff]; being called 1 thread before has finished appending buffstr. need lock around append.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -