0

I have a question about ARC nad NSMutableArray.

Here's the case:

I have a ListView with a NSMUtableArray (arr1) that contains all the elements of the listview. A separate thread, which runs in native code, makes callbacks into objective-c ListView. The native code creates a new NSMutableArray (arr2), fills it with elements of my custom class (each element has a name, id, icon etc), then passes it onto the ListView.

In the ListView, first I clear the array with [arr1 removeAllObjects], then I add each element from arr2 to arr1 with [arr1 addObject: ..].

NOTES:

*All the code, both native and objective-c, is compiled as Objective-C++ code.

*The native code part that allocs and init's arr2 (and all its elements) and calls the ListView stuff is all under @autoreleasepool directive

My questions;

  1. Is there any memory leaks from native code?

  2. Is there any memory leaks from ListView code? Will the old elements I release with [arr1 removeAllObjects] cause memory leaks?

  3. Does @autoreleasepool provide the same functionality as ARC, meaning I wont have to explicitly release the objects?

KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199

2 Answers2

1

Is there any memory leaks from native code?

Not due to lack of release/autorelease (you should look at weak/strong properties, discussion here)

Is there any memory leaks from ListView code? Will the old elements I release with [arr1 removeAllObjects] cause memory leaks?

No.

Does @autoreleasepool provide the same functionality as ARC, meaning I wont have to explicitly release the objects?

You CANNOT release objects with ARC. The autoreleasepool will just ensure that the objects that were allocated within it are destroyed after the closing curly bracket (useful if you have a for loop that allocates tons of stuff on each pass and you want to ensure that everything is cleaned up between each of them, for example).

Community
  • 1
  • 1
jbat100
  • 16,757
  • 4
  • 45
  • 70
1

1) There shouldn't be, but sometimes there are small leaks (I've seen some with the keychain, and some audio libraries). It isn't your problem for dealing with and in most cases would be impossible to resolve.

2) removeAllObjects does send release to all the objects in the array. You can see this by putting a break point in the dealloc method.

3) yes ARC @autoreleasepool works the same

The situation you've described above looks safe to me.

utahwithak
  • 6,235
  • 2
  • 40
  • 62