I am making a blackjack game in cocos2d-x here is the following code of the same where I am getting crash but can not understant why this is happening whats going wrong ? I tried several things thinking that crash might be because of string memory leak or something
CCSprite* _big = CCSprite::createWithSpriteFrameName(ptr);
Here is the card class card.h
#ifndef __Card_H__
#define __Card_H__
#include "cocos2d.h"
USING_NS_CC;
class Card : public cocos2d::CCSprite
{
public:
int mValue;
int mSuit;
bool mHidden;
int mGameValue;
Card();
~Card();
void init(int value, int suit);
void UnHide();
void Hide();
};
#endif
Card.cpp
#include "Card.h"
Card::Card() {
}
Card::~Card() {
}
void Card::init(int value, int suit) {
mValue = value;
mSuit = suit;
mGameValue = 0;
UnHide();
}
void Card::UnHide() {
mHidden = false;
//if(this)
//this->removeAllChildrenWithCleanup(true);
//this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("card"));
this->initWithSpriteFrameName("card");
char strName[64]={0};
//memset ( strName, 0, sizeof ( strName ));
switch(mSuit){
case 0: sprintf(strName,"%s","club_big");break;
case 1: sprintf(strName,"%s","diamond_big");break;
case 2: sprintf(strName,"%s","spade_big");break;
case 3: sprintf(strName,"%s","heart_big");break;
default :
break;
}
const char *ptr = strdup(strName);
CCSprite* _big = CCSprite::createWithSpriteFrameName(ptr);
delete ptr;
// CCSprite* big = CCSprite::create(strName);
_big->setPosition(ccp(55, 44));
this->addChild(_big);
switch(mSuit){
case 0: sprintf(strName,"%s","club");break;
case 1: sprintf(strName,"%s","diamond");break;
case 2: sprintf(strName,"%s","spade");break;
case 3: sprintf(strName,"%s","heart");break;
default :
break;
}
const char *_ptr = strdup(strName);
CCSprite* _small = CCSprite::createWithSpriteFrameName(_ptr);
delete _ptr;
// CCSprite* small = CCSprite::create(str);
_small->setPosition(ccp(16, 82));
this->addChild(_small);
if(mSuit==0 || mSuit==2){
switch(mValue){
case 1: sprintf(strName,"%s","blacka");mGameValue = 1;break;
case 2: sprintf(strName,"%s","black2");mGameValue = mValue;break;
case 3: sprintf(strName,"%s","black3");mGameValue = mValue;break;
case 4: sprintf(strName,"%s","black4");mGameValue = mValue;break;
case 5: sprintf(strName,"%s","black5");mGameValue = mValue;break;
case 6: sprintf(strName,"%s","black6");mGameValue = mValue;break;
case 7: sprintf(strName,"%s","black7");mGameValue = mValue;break;
case 8: sprintf(strName,"%s","black8");mGameValue = mValue;break;
case 9: sprintf(strName,"%s","black9");mGameValue = mValue;break;
case 10: sprintf(strName,"%s","black10");mGameValue = mValue;break;
case 11: sprintf(strName,"%s","blackj");mGameValue = 10;break;
case 12: sprintf(strName,"%s","blackq");mGameValue = 10;break;
case 13: sprintf(strName,"%s","blackk");mGameValue = 10;break;
default :
break;
}
}
else {
switch(mValue){
case 1: sprintf(strName,"%s","reda");mGameValue = 1;break;
case 2: sprintf(strName,"%s","red2");mGameValue = mValue;break;
case 3: sprintf(strName,"%s","red3");mGameValue = mValue;break;
case 4: sprintf(strName,"%s","red4");mGameValue = mValue;break;
case 5: sprintf(strName,"%s","red5");mGameValue = mValue;break;
case 6: sprintf(strName,"%s","red6");mGameValue = mValue;break;
case 7: sprintf(strName,"%s","red7");mGameValue = mValue;break;
case 8: sprintf(strName,"%s","red8");mGameValue = mValue;break;
case 9: sprintf(strName,"%s","red9");mGameValue = mValue;break;
case 10: sprintf(strName,"%s","red10");mGameValue = mValue;break;
case 11: sprintf(strName,"%s","redj");mGameValue = 10;break;
case 12: sprintf(strName,"%s","redq");mGameValue = 10;break;
case 13: sprintf(strName,"%s","redk");mGameValue = 10;break;
default :
break;
}
}
const char *_ptrr = strdup(strName);
CCSprite* _num = CCSprite::createWithSpriteFrameName(_ptrr);
delete _ptrr;
// CCSprite* _num = CCSprite::create(strName);
_num->setPosition(ccp(18, 108));
this->addChild(_num);
}
void Card::Hide() {
mHidden = true;
this->removeAllChildrenWithCleanup(true);
this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("deck"));
//this->initWithSpriteFrameName("deck");
}
Deck.h which is initializing the
#ifndef __Deck_H__
#define __Deck_H__
#include "Card.h"
class Deck
{
public:
static int mCardWidth;
static int mCardHeight;
Card** mCard;
int mCardCount;
Deck();
~Deck();
void init();
void PushCard(Card* card);
Card* PopCard();
bool Empty();
int GetCount();
void RestartDeck();
void Shuffle();
};
#endif
and this is deck. cpp where I am intializing the Card
#include "Deck.h"
#include <iostream>
USING_NS_CC;
USING_NS_CC_EXT;
int Deck::mCardWidth = 0;
int Deck::mCardHeight = 0;
Deck::Deck() {
}
Deck::~Deck() {
delete mCard;
}
void Deck::init() {
CCLog("mCard init");
mCard = NULL;
mCardCount = 52;
if(!mCard)
{
CCLog("mCard initialization");
mCard = new Card*[52];
}
for (int suit = 0; suit < 4; suit++) {
for (int value = 0; value < 13; value++) {
Card* card = new Card;
//card->init
card->init(value+1, suit);
mCard[suit*13 + value] = card;
}
}
mCardWidth = mCard[mCardCount-1]->getContentSize().width;
mCardHeight = mCard[mCardCount-1]->getContentSize().height;
Shuffle();
}
void Deck::PushCard(Card* card) {
mCard[mCardCount++] = card;
}
Card* Deck::PopCard() {
if (mCardCount > 0) {
return mCard[--mCardCount];
}
return NULL;
}
bool Deck::Empty() {
return mCardCount == 0;
}
int Deck::GetCount() {
return mCardCount;
}
void Deck::RestartDeck(){
mCardCount = 52;
Shuffle();
}
void Deck::Shuffle() {
// std::random_shuffle(mCard, mCard+52);
CCLog("mCard Shuffle");
int visited[52] = {0},num_array[52] = {0};
int count = 0;
srand(time(0));
while (count < 52) {
int num = rand() % 52;
if ( visited[num] == 0 ) {
visited[num] = 1;
num_array[count++] = num;
}
}
count=0;
int lastIdx = mCardCount - 1;
int swapIdx;
Card* swapCard;
while (lastIdx > -1) {
swapIdx = num_array[count++];
swapCard = mCard[swapIdx];
mCard[swapIdx] = mCard[lastIdx];
mCard[lastIdx] = swapCard;
lastIdx--;
}
}
The crash I get is on the line of initWithSpriteFrameName but it random crash.
I guess its memory leak please help me with the same