Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nicsoft

macrumors member
Original poster
Oct 17, 2009
53
0
Stockholm
Hello,

I have created a program and are cleaning up in order to not create any memory-leaks. Since I am a beginner I made some incorrect decisions in the beginning when I started creating this application, which I do in order to learn how to develop Objective-C/Cocoa-applications. I'm also new to C...

I did create a C-2Darray (MyObject *myObjArr[x][y];) with known x- and y-values. The objects are my own custom objects. The objects are created like [[MyObject alloc] init];

Now, I am not sure how to realease the memory since it is a mixture of C and Objective-C. I know that Objective-C is a superset of C, but still not sure about the proper way of handling the freeing of memory.

Should I

1. Loop through all objects and do [myObjArr[x][y] release];
2. delete [][] bric2 [x]; //For each x
3. delete [] bric2;

Thanks in advance!
 
You shouldn't mix Objective-C, C and C++, because each language has his own policies regarding allocating and deallocating memory. For example, if you use a free() function with an ObjC or C++ object, his destructor is never called.

If you built the objects using [[ ... alloc] init], you shouldn't use delete [] ( which is actually C++ ), or free().

You should loop through the objects and call release over each. Something like:

Code:
for ( j = 0; j < ... )
  for ( i = 0; i <.... )
   [myObject[j][i] release];

Even if you don't release the objects, if you are using a garbage collector, it should reclaim unreferenced memory though
 
Thanks for your reply. No garbage collection is possible since I am developing for iPhone...

Your example is according to my first point.

But do I need to release/delete the pointers in some way? MyObj[x] is a pointing to an array of object-pointers (I assume that's how to say it...). And MyObj is pointing to an array of pointers as well. Isn't there any need to release those two?
 
Thanks for your reply. No garbage collection is possible since I am developing for iPhone...

Your example is according to my first point.

But do I need to release/delete the pointers in some way? MyObj[x] is a pointing to an array of object-pointers (I assume that's how to say it...). And MyObj is pointing to an array of pointers as well. Isn't there any need to release those two?

You have to release the objects that your pointers point to.

You don't have to release the pointers themselves. The pointers are local variables that are automatically freed when they go out of scope.
 
Just as a side note. You might think about using an NSMutableArray. I assume that you aren't creating a huge number of objects if your developing for the iPhone. You could then implement 2-D indexing using macro's or a category. By using an NSMutableArray you'll get a number of features without having to implement them yourself, such as memory management, archiving, copying, etc.

crackpip
 
Just as a side note. You might think about using an NSMutableArray. I assume that you aren't creating a huge number of objects if your developing for the iPhone. You could then implement 2-D indexing using macro's or a category. By using an NSMutableArray you'll get a number of features without having to implement them yourself, such as memory management, archiving, copying, etc.

crackpip

on a similar note, an NS(Mutable)Array holds any NSObject, including other NS(Mutable)Array's. This means you could have an "outer" array, with each element being an "inner" array. This would allow you to have a jagged array (the length of each "inner" array could have different lengths) and releasing would be one release on the "outer" array. When it is deallocated all of it's members, the "inner" arrays, will be released, and then all of their members will be released on their subsequent deallocation.

Access to an individual member of an "inner" array would be a bit verbose, but a helper function, utility method, category, etc. could resolve this for you.

-Lee
 
I second (or third?) what crackpip and lee said. Personally I don't go down to C unless there's a good reason to. The Cocoa APIs are very good and very mature and by going down to C you're giving up the object-oriented nature and runtime of using Objective-C (although it will be somewhat slower). As Lee mentioned, you will probably want to set up some convenience methods as Cocoa doesn't directly support multidimensional arrays, but it should keep your memory management easier. A multidimensional version of NSArray would be really useful though...someone has probably already written that somewhere.

Another option if you don't need to do a lot of deleting and adding elements is to just declare everything in one (one-dimensional) NSArray and then write a helper class to figure out the correct offset of the elements to return when you feed it x and y (row and column) components.
 
You could create a subclass of NSArray that would convert "rectangular" references into a linear index, with range checking so that nil is returned when values are out of range and to pad insertions for accurate placement. In such a case, it would not be necessary to use NSMutableArray as a superclass because you would probably not use any of its methods or would have to override them. Before doing that, however, you might just find it easier to just do the co-ordinate to index translation in your accessing code, because NSArray is a class cluster that requires you to provide your own backing storage for your subclass.

One very important thing to remember about NSMutableArray is that when you add an object, the array sends the object a retain and when you remove an object, a release is sent. If you create an object with -new, -copy or -mutableCopy and you want it to evaporate when you release the array, you need to release the object after adding it or it will leak (for this reason, you can only add valid, non-nil objects to an array).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.