CCArray persistence problem

Hello,

I’ve asked this earlier, but I thought I would pose the question again to see if someone can assist. I’m creating a CCArray to store some values for a selection screen. I create the CCArray in the init, and a CCLOG can retrieve the value of one of the entries. Later on, I try and retrieve a value from the array based on the user’s selection, but the program fails reporting an out of range error. A CCLOG for testing the array~~>count fails as well. It seems based on this that even though my CCArray is global, the values aren’t persistent past some point, but I’m not sure what point that is. Example is below:
<pre>
File.h
CCArray *items;

File.cpp
bool File::init{
items = CCArray::create;
items~~>addObject(example1);
items~~>addObject;
CCLOG (“count: %i”, items->count());//returns 2
this~~>method1();
}

void File::method1(){
CCLOG (“count: %i”, items->count());//returns 2
}

//then later on, on a button press…
void File::method1(){
CCLOG (“count: %i”, items->count());//fails with error or returns something large or incorrect
}
…CCLOG is just for example— referencing any values in the array causes a crash. Not really sure why I get a correct number from the first two but an unexpected result from the

last one. Anybody have any thoughts? Adding the CCArray to my Global Value Singleton class didn’t fix the problem for me, so I tried creating the CCArray itself as the singleton, and it didn’t work either. (I should point out that I’m new to C++ and cocos2dx, so this may be a familiar issue) At this point, I’m open to any suggestions. I’m sure I can come up with some ridiculous workaround solution to at least get my game working, but I’d love to fix this issue. My current thought it that I’ll just build the CCArray again later on using all the same values, but to me this seems like an unsatisfactory solution.

Well, item = CCArray::create() concludes that “item” is an auto-release object.
You should call item->retain() in after create(), and manually item->release() when you don’t need it.
Please read [[Memory Management in Cocos2d-x]] for the details and mechanics.

Walzer Wang wrote:

Well, item = CCArray::create() concludes that “item” is an auto-release object.
You should call item->retain() in after create(), and manually item->release() when you don’t need it.
Please read [[Memory Management in Cocos2d-x]] for the details and mechanics.

Walzer, thank you very much for your help— that worked perfectly and fixed my problem.

is CCArray::create()->retain() equals new CCArray?

A little difference:

CCArray::create()   // reference = 1, autorelease flag was set.      |     new CCArray()    // reference = 1, autorelease flag was not set.
->retain()             // reference = 2, autorelease flag was set.      |

After current frame finished,  
                            // reference = 1, autorelease flag was clear.     |                          

But anyway, it seems to be the same, the difference is CCArray::create->retain* decrease its reference to 1 after current frame finished whilenew CCArray* ’s reference is always 1.
GuangHai Lin wrote:

is CCArray::create()->retain() equals new CCArray?

Great Explanation.I totally understand it !thanks very much::wink:

is there any deep inside reason why so many methods are deprecated? I have been working on an cocos2d game,it’s almost finished.now i want to port it to the cocos2d-x,i found so many methods have changed.especially static constructor,the newer version of cocos2d-x use lots of ::Create(), Why?

GuangHai Lin wrote:

is there any deep inside reason why so many methods are deprecated? I have been working on an cocos2d game,it’s almost finished.now i want to port it to the cocos2d-x,i found so many methods have changed.especially static constructor,the newer version of cocos2d-x use lots of ::Create(), Why?

I think it’s to take advantage of function overloading. Objective-c does not support function overloading, so there are many different names for functions that do the same thing but with different parameters. Now those functions are being replaced for overloaded ones…. which is a great feature of c++.

thanks very much.