2012-07-12 98 views
1

我使用了cocos2d-2.0-RC2-X-2.0.1 @ 2012年6月29日寫了這樣CCArray 「訪問衝突讀取位置」

.H

... 
protected: 
CCArray *array; 
... 

的.cpp

... 
bool HelloWorld::init() 
{ 
... 
array= CCArray::create(2); 
array->addObject(obj1); 
array->addObject(obj2); 
... 
} 

void HelloWorld::ccTouchesBegan(CCSet* touches, CCEvent* event) 
{ 
    CCLog("%i", array->count()); 
} 
... 

並得到這個: 0xC0000005:訪問衝突讀取位置「0xfeeefeee」。

CCArray.cpp

unsigned int CCArray::count() 
{ 
    return data->num; 
} 

請幫助。

+0

像這個0xfeeefeee這樣的幻數通常指向一個解除分配的對象或超出範圍的問題。例如,malloc guard和其他調試設置使用這種模式來檢測緩衝區覆蓋。 – LearnCocos2D 2012-07-12 19:45:48

回答

4

嘗試調用

array->retain() 
後創建

。在離開函數後,陣列可能會自動釋放。

但不要忘記在完成後釋放它。

0

你也可以這樣做:

array = new CCArray(); 
array->initWithCapacity(3); 

create()返回autorelease CCObject。