我一直面临这个问题,我承认我缺乏一些内存管理的基本概念。我无法解决这个问题并相信我,我一直在尝试很多事情。
在我的应用程序中,有 4 个线程(将来最多可以有 12 个线程)将从 RS232 端口读取。在每个线程 ( threadRS232Read
) 中,我将使用 appendString 将 RS232 字符附加到相应的 NSMutableString 中。NSMutableString 将与 appendString 一起变得非常大,直到完成完整的测试。并定期在里面进行一次测试,clearRdBuffStr
调用清除字符串。该应用程序总是在 appendString 中崩溃。如果幸运的话,我可以运行一些测试,但通常情况下,它会在第一次运行时崩溃。以下是代码片段和崩溃日志。
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];`
在另一个文件 RS232RW.m 中,将使用一个线程将 buffStr1 更新为 4
RS232RW.m
- (void)threadRS232Read:(id)argument {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
....
//read from RS232 port buffer and 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 will be called by the other part of the program to clear this 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;
....
应用程序总是在上面的 appendString 的 1 处崩溃。
崩溃日志如下:
....
Crashed Thread: 3
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Application Specific Information:
objc[584]: garbage collection is OFF
*** error for 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