views:

39

answers:

3

I am having a problem with memory management I think. My NSArray (called arr) is turning into different things at random times. I have no idea why. I have a .h file that that declares the array, and then I initialize the array using

NSString *input = [[NSString alloc] initWithData:myData encoding:NSACIIStringEncoding];
arr = [input componentsSeperatedByString:@"\n"];

and then I use it throughout the program and suddenly it changes into different things (UITouch, sometimes, for example). I never called a release on it. Why is it doing this? How do I prevent objects from randomly being changed due to memory issues?

Thanks!

A: 

Try to retain arr.

arr = [[input componentsSeperatedByString:@"\n"] retain];
Espuz
This is correct, the reason this happens is because 'componentsSeperatedByString' returns an autoreleased object. Once the array retain count hits zero the object is deallocated and that memory is now free to be occupied by the next object that needs it. Hence why you seems it is "turning into different things".
nick
But now I am getting memory leaks.
Robert Eisinger
@Robert: always try to understand things instead of blindly following advice from strangers (like in the real world)
mvds
I did try to understand it. However I believe it has to do with this method "components seperated by string." I understand the ideas behind memory release it is just that I'm not an expert. I come from the world of Java, where memory management is built in and is more smart than it is here. Now, how can I prevent this memory leak? I initialize the array once, and I release it in the dealloc. How are there leaks?
Robert Eisinger
"more smart" could also be "more dumb" depending on the perspective. I cannot see what happens in your code on the larger scale. Maybe you run this line more than once, in which case you *will* have a leak since you don't release it as many times. Using the getter/setter approach (as in my answer, which you maybe found too long to read, but did get all the upvotes) this is solved automagically.
mvds
I didn't quite understand your post. I did read it all though. Would you mind explaining yourself a little better?
Robert Eisinger
@Robert: I've been saying more than 1 kb here, maybe you should formulate a question, about what statement or aspect of the system you don't understand, rather than asking me to elaborate even more. There's one thing I left out on purpose, to keep things a little cleaner, and that's `autorelease`. Just search around for it (here and/or in google), there's tons of questions about cocoa memory management. Most answers are high quality.
mvds
A: 

Or Initalize a new Array with this array:

arr = [[NSArray alloc] initWithArray:input];
Sandro Meier
thus introducing a memory leak.
mvds
Oh yes that's right. You need to release it anywhere...
Sandro Meier
+2  A: 

What happens, is that the memory, once occupied by your NSArray, is occupied by another object. This may be any object, and since you're touching the screen a lot a UITouch is very common.

This means you're not retaining the NSArray when you should, so it is released prematurely. You don't show the code declaring arr, but if you declare arr as

@property (nonatomic,retain) NSArray *arr;

and synthesize it using

@synthesize arr;

then the retaining is handled by simply assiging to self.arr instead of arr:

self.arr = [input componentsSeperatedByString:@"\n"];

In cocoa-speak, your object now "owns" the array. In the dealloc method of this class, you should [self.arr release].

Should you assign some other array to self.arr, the object assigned to self.arr previously will get released, and the new one retained.

mvds