issue #13: allocate memory for member variable m_pszName

This commit is contained in:
Ming 2010-08-03 02:25:38 +00:00
parent 40abd44425
commit 80c347932e
2 changed files with 17 additions and 2 deletions

View File

@ -30,6 +30,8 @@ THE SOFTWARE.
#include "cocoa/NSMutableArray.h"
#include "cocoa/NSObject.h"
#include <string.h>
class CGRect;
class CGPoint;
class CGSize;
@ -118,7 +120,17 @@ public:
// name of the animation
inline char* getName(void) { return m_pszName; }
inline void setName(char *pszName) { m_pszName = pszName; }
inline void setName(const char *pszName)
{
if (m_pszName)
{
delete m_pszName;
}
m_pszName = new char[strlen(pszName) + 1];
memcpy(m_pszName, pszName, strlen(pszName));
m_pszName[strlen(pszName)] = '\0';
}
// delay between frames in seconds
inline float getDelay(void) { return m_fDelay; }

View File

@ -84,7 +84,9 @@ CCAnimation* CCAnimation::initWithName(const char *pszName, NSMutableArray<CCSpr
CCAnimation* CCAnimation::initWithName(const char *pszName, float fDelay, NSMutableArray<CCSpriteFrame*> *pFrames)
{
m_fDelay = fDelay;
m_pszName = const_cast<char*>(pszName);
m_pszName = new char[strlen(pszName) + 1];
memcpy(m_pszName, pszName, strlen(pszName));
m_pszName[strlen(pszName)] = '\0';
m_pobFrames = NSMutableArray<CCSpriteFrame*>::arrayWithArray(pFrames);
return this;
@ -94,6 +96,7 @@ CCAnimation::~CCAnimation(void)
{
CCLOGINFO("cocos2d, deallocing %p", this);
// [name_ release];
delete m_pszName;
m_pobFrames->release();
}