IOS NSIndexPath ,内存问题
一直对IOS的内存管理这块不是很了解,等忙完手上的项目,就抓紧时间梳理下。
昨天写程序遇到一个问题,关于NSIndexPath的。程序中有段代码是这样的:
self.lastIndexPath = [[NSIndexPath alloc] init];
NSIndexPath *temp = [NSIndexPathindexPathForRow:self.selectRowinSection:1];
self.lastIndexPath = temp;
[temp release];
然后程序就运行中在 @synthesize lastIndexPath 的 地方报一个错: thread1 : program received a signal :"EXC_BAD_ACCESS";
公司软件总监说这是用了已经释放了的对象造成的,我百度到的也大都说是这个原因。
因为自己这段程序上有这样一段类似的代码:
NSArray *array = [[NSArray alloc] initWithObjects:@"透明",@"白色",@"淡绿",@"粉红",@"浅紫",nil];
self.colorSelectList = array;
[array release];
但这个地方的 self.colorSelecyList 是没有alloc init 的 ,而是将一个 alloc init 的array的 对象赋给它。开始以为2个地方是一样的,没什么问题。
后来看到http://www.cocoachina.com/bbs/read.php?tid=90991 说没有retain。
我就开始怀疑这里真的有问题,我试着把 [temp release];这句注释掉。发现程序就不报错了。
还有一种办法: self.lastIndexPath = [temp retain]; [temp release]; 这时可以不注释 这句 temp release。
还不清楚 这些具体的区别,改天好好补一下内存管理。