How to release a array which is created inside a for loop in Objective C?
我在 for 循环中创建了一个数组,但无法释放它。它显示内存泄漏。这是我的代码:
1 2 3 4 5 6 | for(int i = 0; i < [magArr count]; i++) { Magazine *magObj = [magArr objectAtIndex:i]; NSMutableArray *myArray = [data readEditions:[magObj.magazineID intValue]:0];//returns a array } [myArray release]; // memory leaks (retain count to -1) |
如果我也使用自动释放,它会显示内存泄漏。如何解决这个泄漏?
如果从
你应该释放循环内的对象,因为如果
您应该从
然后,如果您愿意,您可以
下面是一个例子,如果
1 2 3 4 5 6 7 | for(int i = 0; i < [magArr count]; i++) { Magazine *magObj = [magArr objectAtIndex:i]; NSMutableArray *myArray = [[data readEditions:[magObj.magazineID intValue]:0] retain]; //do something with myArray... [myArray release]; } |