What is the best a quickest way to reset an NSMutableArray?
-[NSMutableArray removeAllObjects] doesn't work for you?
removeAllObjects
removeAllObjects if assuming by 'reset', you mean you just want to empty the array.
If you are attempting to do what I think you are attempting to do, which is to keep an array empty but not release it, or at least to make it available next time it is needed then firstly you need to set a variable or a property within your class for this variable:
NSMutableArray *mutableArray;
Next add this code before the position at which you will need the empty array:
if (!mutableArray) {
mutableArray = [[NSMutableArray alloc] init];
}
Now you can safely call
[mutableArray removeAllObjects];
without fear that the array will become unavailable once empty.
Related
I'll try to be as much clear as possible:
I create 2 MutableDictionary
I add to both of them the same NSMutableArray object:
[self.myList setObject:tempC forKey:keyV];
[self.listFiltered setObject:tempC forKey:keyV];
In other part of the code, I want to empty one, so I do:
[self.listFiltered objectForKey:keyV] removeAllObjects];
The problem is that the objects are being removed in BOTH mutableDictionaries!
The same NSNSMutableArray is added to both dictionaries. It doesn't matter whether you access it through one dictionary or the other, you end up manipulating the same NSMutableArray instance.
To fix this, you need store different NSMutableArray instances in each dictionary. The easiest way to do this is through [NSMutableArray copy], which will do a shallow copy.
Lastly, naming dictionaries with names ending in list is a bad practice.
You need to copy the array to the other dictionary.
See here on how you can go about this.
Both objects are same... you only provide a pointer to that mutablearray to both the dictionary so when you delete in one the other also gets deleted .. what you need is to store copy of that array into your dictionary... hoping this helps.
you can add the array like this..(Seems to me you want a deep copy)
NSMutableArray *newArrayOne = [[NSMutableArray alloc] initWithArray:tempC copyItems:YES];
[self.myList setObject:newArrayOne forKey:keyV];
[newArrayOne release];
NSMutableArray *newArrayTwo = [[NSMutableArray alloc] initWithArray:tempC copyItems:YES];
[self.listFiltered setObject:newArrayTwo forKey:keyV];
[newArrayTwo release];
this way two different objects are stored in there.. this is not the most optimized code.. it is just to make you understand what actually is happening behind the scenes.
I am using a search bar in my app and display some results below from an ext API.
The contents first get stored in an array "xyz" and each time the user types in the search bar, I removeAllObjects and reload the table.
The results are correct if the user types slow i.e. [xyz removeAllObjects] works fine...However if the user types very fast, [xyz removeAllObjects] does not seem to have any effect and duplicate items get appended to the array..
I am not sure how to fix this. Please help me. Thank you.
removeAllObjects is an entirely serial operation; that method doesn't return until the array is empty.
Therefore, there must be a thread in play and you are quite likely accessing a mutable array from multiple threads. Mutable arrays aren't thread safe. Fix the exclusivity and you'll fix your problem.
The easiest way is to separate the array being displayed from the array being computed. As soon as the computation is done, move the computed array to the display array and never mutate it again.
Why not create a new NSArray, point the results at that, and then release the old array. That way having duplicates will be impossible. Something like:
NSArray *newArray = [someObject newSearchResults];
NSArray *oldArray = xyz;
xyz = [newArray retain];
[oldArray release];
An array has contains multiple objects.If we call removeAllObjects method, Whether objects will
be released which in an array or we have release manually.
Sending an NSArray that message will cause it to release all its pointers. You don't need to release what you put in there yourself, unless you have your own pointers to those objects elsewhere.
You shouldn't need to remove all objects before you release. When an NSMutableArray is dealloc'd, it releases all of the objects in contains automatically.
More click here
You should do this to release your array. Better to release it this way rather than autoreleasing the array
[YourArray release];
YourArray = nil;
If you just want to get rid of the data and dont want to release the array then you can simply do:
YourArray = nil;
It is better to release array using [YourArray release] statement then declaring it as autorelease in declaration.
Hope this helps you.
[sender LoadDataComplete:arrDetailData];
I am returning this array like this from an objective C class in my my class which inherits ViewController...if I write [arrDetailData release] below this...i get crash...
Please advise on it...how to get this array without a leak
Why don't you autorelease it ?
#devaditya you should use autorelease instead of release
[arrDetailData autorelease];
you can write this statement before the return statement.
when you are using [array relese] it will release the object very soon it may crash your app..so use autorelese..If you mean you want to remove the objects from array use [array RemoveAllobjects].. Refer about memory management
What's the right way to do this?
I have an array that I will use on several methods. I will add objects to it, get values, replace values, etc.
Today I do this:
I declare it on .h, using something like
NSMutableArray *myArray;
as soon as the application starts, I declare it on .m doing something like
myArray = [[[NSArray alloc] init] retain];
If I don't add the retain the array will be released at some point and the application will crash. But allocating the array at the beginning of the application and left it "open" without releasing it will make instruments cry, pointing the finger at me, calling me a "leaker"...
How to solve that? Is this the correct way to do that? how do you guys do stuff like this?
thanks
alloc implicitly sets the retain count to 1. By sending the retain message you're incrementing the retain count to 2. In order for the object to be deallocated you would then need to release it twice. Failure to do so would result in a memory leak.
Ideally you should create the object in your init method using [[NSArray alloc] init] and then release it in your dealloc method like so:
- (void)dealloc {
[myArray release];
[super dealloc];
}
You might also find this article useful: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
One more thing: You declared myArray as an NSMutableArray but instantiated it as an NSArray. Perhaps that's causing the crash.
You should not retain the object you've just created. You already own it. If, as you say, "the array will be released at some point and the application will crash," that is the code you should change. Your code shouldn't be releasing an object that you still want to keep around.