많은 버튼을 추가하려는 CCLayer가 있습니다.내 객체에 cclayer를 전달할 때 메모리 문제가 발생했습니다.
-(id) initWithLayer:(MyLayer *)theLayer andDictionary:(NSDictionary *)data {
self = [super init];
if (self != nil)
{
self.layer = theLayer;
self.tag = [[data objectForKey:@"day"] intValue];
_layerZ = [[data objectForKey:@"z"] intValue];
_closedCard = [[CCSprite alloc] initWithFile:[data objectForKey:@"file"]];
_openedCardFile = [[NSString alloc] initWithString:[data objectForKey:@"open_card"]];
CCMenuItemImage *itemImage = [CCMenuItemImage
itemFromNormalSprite:_closedCard
selectedSprite:nil
target:self
selector:@selector(displayCard:)];
CGPoint position = CGPointFromString([data objectForKey:@"position"]);
_position = ccp(position.x, position.y);
itemImage.position = _position;
itemImage.tag = self.tag;
_theCardButton = [CCMenu menuWithItems:itemImage, nil];
_theCardButton.position = CGPointZero;
[self.layer addChild:_theCardButton z:_layerZ];
}
return self;
}
이 초기화 기능은 레이어에 버튼을 추가하고 나는 MyButton에있는 모든 행동 (움직임, 이미지 변경) 관리 해요 : 나는이 init 함수와 MyButton 클래스를 생성 수업.
그래서 모든 MyButton 인스턴스로 레이어가 전송됩니다. 레이어가 각 버튼을 놓을 때까지 dealloc 메서드를 호출하지 않습니다. 나는이 장면을 교체 할 때
_buttons = [[NSMutableArray alloc] init];
for (NSDictionary *buttonData in buttonsArray) {
MyButton *btn = [[MyButton alloc] initWithLayer:self andDictionary:buttonData];
[_buttons addObject:btn];
[btn release];
}
내 문제는, MyButton에의 할당 해제의 Methos는 호출하지 않을되어 있고 메모리가 해제되지 않는 :
이 버튼 배열을 통해 레이어에 추가되고있다 [_buttons release]가 호출되지 않는 레이어의 dealloc 메서드에 있기 때문에 레이어가 각 버튼에 전달되었습니다.
엉망입니다. 누구든지이 메모리 문제를 해결하는 가장 좋은 방법을 말해 줄 수 있습니까?
- 편집
내가 수동으로 replaceScene를 호출하기 전에 버튼의 배열을 해제하려고하면 각 버튼의 dealloc을 호출하지만 나는이 오류 메시지가 얻을 :
[CCMenu pauseSchedulerAndActions]: message sent to deallocated instance 0x131a0670
보유주기를 만들었습니다. init에 레이어를 전달하지 마십시오. 레이어가 노드의 부모 인 경우 parent 속성을 사용할 수 있습니다. 다른 모든 상황에서는 레이어를 싱글 톤 클래스로 만드는 것을 고려하십시오. – LearnCocos2D