views:

376

answers:

3

I have gone through the following question.

http://stackoverflow.com/questions/1528696/objective-c-where-do-you-dealloc-global-static-variables

But the question is related on static variables. It has something different situation then mine.

I have following code in application.

//.h file
#import "cocos2d.h"
#import "something.h"
#import "myLayer.h"
#import "LayerData.h"

// i have taken this variables as global 
// because two different classes are simultaneously accessing it

myLayer *myCurrentLayer;
LayerData *boxLayerData[10][12];

@interface one
    // my class definition here
@end

@interface two
    // my second class definition here
@end


//------------------------------------------------
@implementation one
    // my class constructor here.
    -(id)init{
        myCurrentLayer=[[myLayer alloc] init];
        // boxLayerData is initialized with objects
    }
@end

@implementation two
    // second class constructor
    -(id)init{
        [myCurrentLayer setPosition:ccp(10,20)];
        [self schedule something for movements];
    }
@end
//------------------------------------------------

Ok. A confusion is "how to dealloc 120 sized "LayerData *boxLayerData[10][12];" array ?"

Plz. Give some suggestion.

Thanks in advance.

Sagar.

+1  A: 

boxLayerData in the code above seems to be an array of pointers. While the pointers may need to be managed by some means, the array that is storing those pointers does not need to be, and will be properly disposed of at application termination.

fbrereto
sugar
+5  A: 

The same answer applies to global as it is to static. If you need the data for the entire application lifecycle, just leave it as it is and the memory will be reclaimed by the OS when the app terminates. If you need to release the object during the application's execution, you can loop through the array and call release on each object.

Something like this:

for (int i = 0; i < 10; i++) { for (int j = 0; j < 12; j++) { LayerData *data = boxLayerData[i][j]; [data release], data = nil; } }

Boon
sugar
Ok. I thought this is the only way. So, Finally I followed this way.
sugar
be careful to check that there's an object at a[n][m].
pxl
I have implemented something like this. if(boxLayerdata[i][j]!=nil){[boxLayerData[i][j] release];boxLayerData[i][j]=nil;}
sugar
There is no need to check for nil. In Objective-C, sending a message to nil does nothing, so the check is not needed.
Boon
+2  A: 

you're concerned with a user aborting and restarting a level. have you considered creating a singleton class that will store all this stored/shared data instead of a global?

this way you can design access to this data such that you don't write new data before its set or try to access something that was just recently deallocated, define whether the array elements are accessed atomically, ensure thread safety, galore.

pxl
Yup ! you are right. I am aware of it. When I started development, I had just a single value for the global. While developing game, slowly my demands for global increased. Finally I have array of 120 as global.I know this isn't a good practice. But Right now, for me the game completion is important. So I asked question here if any alternate solution available.
sugar
totally understand.
pxl