How to release a C-2darray

Discussion in 'Mac Programming' started by Nicsoft, Dec 28, 2009.

  1. Nicsoft macrumors member

    Joined:
    Oct 17, 2009
    Location:
    Stockholm
    #1
    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!
     
  2. robj macrumors regular

    Joined:
    Dec 23, 2009
    Location:
    Madrid, Spain
    #2
    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
     
  3. Nicsoft thread starter macrumors member

    Joined:
    Oct 17, 2009
    Location:
    Stockholm
    #3
    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?
     
  4. robj macrumors regular

    Joined:
    Dec 23, 2009
    Location:
    Madrid, Spain
    #4
    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.
     
  5. Nicsoft thread starter macrumors member

    Joined:
    Oct 17, 2009
    Location:
    Stockholm
  6. crackpip macrumors regular

    Joined:
    Jul 23, 2002
    #6
    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
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    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
     
  8. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #8
    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.
     
  9. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #9
    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).
     

Share This Page