I am trying to empty an NSMutableArray with the [myarray removeallobjects]; but I am getting error exc_bad_access. Is this the right way to empty the array? I tried to set it to nil but its not working either. Actually what I am doing is filling the array with data and the user has the option to "refresh" the data and I want to empty the array before enter the refreshed data. I cant post any code because is too bi开发者_C百科g.
-[NSMutableArray removeAllObjects]
is the correct way of emptying an NSMutableArray
. You're most likely getting a crash because you're still using the objects that you removed somewhere, in your UI perhaps.
I think the problem is in this snippet of code that you've provided in the comments,
[checkinArray addObject:checkinsA];
[checkinsA.taggedID release];
[checkinsA.taggedName release];
[checkinsA release];
taggedID
and taggedName
are properties of the checkinsA
object. They should be released in the dealloc
method only. The array does not retain the object tree. It retains the root object only. So there shouldn't be a release here. So knock out the two lines in the middle and make it
[checkinArray addObject:checkinsA];
[checkinsA release];
You are having a crash because your array is not existing anymore in memory.
I bet it is because you forgot to retain it (or released it too soon)
You probably created it using [NSMutableArray array]
or arrayWithObjects:
or some of those variants, or called autorelease
after your alloc
+init
, whereas if it is an instance variable you should retain it until the objet is dealloc'd.
Note that memory mgmt is a key part of iOS development. Be sure to know it perfectly (such as it becomes natural and you won't hesitate when to retain and when to release) before going further, it will avoid you a lot of trouble like this once you use it right.
精彩评论