I have this :
-(NSMutableArray*)setManch:(NSMutableArray*)data
{
NSMutableArray *manch=[data mutableCopy] ;
// some calculations on manch
return manch;
}
If i try to do :
NSMutableArray *manch=[[data mutableCopy] autorelease] ;
I get a crash .
But, i have to release this copy somehow because I call this function again and again. How should I do that?
for(int k=0;k< count;k=k+2)
{
if(k==count-1)
[manch addObject:[NSNumber numberWithInt:![[manch objectAtIndex:k] integerValue] ] ];
else
[manch insertObject:[NSNumber numberWithInt:![[manch objectAtIndex:k] integerValue] ] atIndex:k+1 ];
}
I would recommend to use ARC.
But anyway. The autorelease is correct here. So your mistake is probably somewhere else. What does the error state exactly?
Probably the problem is the data IN your array. A simple copy call does NOT make deep copies. So if you’re objects only exist in the first array, they will be released as soon as that array gets released. Debug your data: look what exists before and after that method call.
You should read about & understand memory management on ios:
General Memory Management:
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html#//apple_ref/doc/uid/10000011i
Practical Management, especially “Avoid Causing Deallocation of Objects You’re Using”
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/20000043-1000922
Also read about ARC:
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html#//apple_ref/doc/uid/10000011i