为了方便起见,我用一些工作代码编辑了上一篇文章。
以下代码(ARC'ed)似乎会泄漏,并且会在运行一小段时间后使 sim 卡或设备崩溃:
#define kROWS 100
#define kCols 34
void run();
static ViewController *instance;
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *nsBackColor;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.nsBackColor = [NSMutableArray arrayWithCapacity:1];
instance = self;
// set up a '2D array'
for (int x = 0; x < kROWS; x++) {
[self.nsBackColor addObject:[NSMutableArray arrayWithCapacity:1]];
for (int y = 0; y < kCols; y++) {
[[self.nsBackColor objectAtIndex:x] addObject:[UIColor whiteColor]];
}
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
run();
});
}
- (void)plotColor:(UIColor *)color atX:(short)x andY:(short)y {
[[self.nsBackColor objectAtIndex:x] replaceObjectAtIndex:y withObject:color];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
void plot(short xLoc, short yLoc,
short backRed, short backGreen, short backBlue) {
@autoreleasepool {
[instance plotColor:[UIColor colorWithRed:((float)backRed/100)
green:((float)backGreen/100)
blue:((float)backBlue/100)
alpha:(float)1]
atX:xLoc andY:yLoc];
}
}
void run() {
short x = 0;
short y = 0;
short backRed = 0;
short backGreen = 0;
short backBlue = 0;
while (true) {
x++;
if (x >= kROWS) {
x = 0;
}
y++;
if (y >= kCols) {
y = 0;
}
backRed = arc4random() % 255;
backBlue = arc4random() % 255;
backGreen = arc4random() % 255;
plot(x, y, backRed, backGreen, backBlue);
usleep(1000);
}
}
如果我让它在设备或模拟器上运行足够长的时间(几分钟),我将收到 mmap malloc 错误(sim)或内存警告(设备)并崩溃。
通过 Instruments Allocations 我可以看到 +[UIColor colorWithRed:green:blue:alpha:] 开始膨胀,直到最终撞到内存墙。
我可以将 UIColor 分配给一个属性(直接或通过复制),比如 self.myColor = color,并且没有这样的泄漏。
我也可以这样做:
[[self.nsBackColor objectAtIndex:x] replaceObjectAtIndex:y withObject:[self description]];
我得到了同样的泄漏。
在我看来,在数组中替换的对象(是的,这最初是作为 2D c 数组开始的,但我认为这是问题所在)永远丢失并泄漏并且没有正确释放。
这将是运行一小段时间后的 Instruments->Allocations:
任何帮助将不胜感激,并且可以提供更多信息。