Skip to content

Commit d8fe217

Browse files
author
natural-law
committed
[android]fixed cocos2d#356,CCLabelTTF::initWithString not crashed anymore.
1 parent 9336863 commit d8fe217

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

cocos2dx/include/CCLabelTTF.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ namespace cocos2d{
3737
class CC_DLL CCLabelTTF : public CCSprite, public CCLabelProtocol
3838
{
3939
public:
40-
CCLabelTTF()
41-
:m_sFontName("")
42-
{}
43-
virtual ~CCLabelTTF(){ m_sFontName.clear(); }
40+
CCLabelTTF() {}
41+
virtual ~CCLabelTTF(){}
4442
char * description();
4543
/** creates a CCLabelTTF from a fontname, alignment, dimension and font size */
4644
static CCLabelTTF * labelWithString(const char *label, CCSize dimensions, UITextAlignment alignment, const char *fontName, float fontSize);
@@ -61,9 +59,9 @@ namespace cocos2d{
6159
protected:
6260
CCSize m_tDimensions;
6361
UITextAlignment m_eAlignment;
64-
std::string m_sFontName;
62+
ccxScopedPtr<std::string> m_pFontName;
6563
float m_fFontSize;
66-
std::string m_sString;
64+
ccxScopedPtr<std::string> m_pString;
6765
};
6866

6967
} //namespace cocos2d

cocos2dx/label_nodes/CCLabelTTF.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace cocos2d{
5757
{
5858
m_tDimensions = CCSizeMake( dimensions.width * CC_CONTENT_SCALE_FACTOR(), dimensions.height * CC_CONTENT_SCALE_FACTOR() );
5959
m_eAlignment = alignment;
60-
m_sFontName = fontName;
60+
m_pFontName.reset(new std::string(fontName));
6161
m_fFontSize = fontSize * CC_CONTENT_SCALE_FACTOR();
6262
this->setString(label);
6363
return true;
@@ -70,7 +70,7 @@ namespace cocos2d{
7070
if (CCSprite::init())
7171
{
7272
m_tDimensions = CCSizeZero;
73-
m_sFontName = fontName;
73+
m_pFontName.reset(new std::string(fontName));
7474
m_fFontSize = fontSize * CC_CONTENT_SCALE_FACTOR();
7575
this->setString(label);
7676
return true;
@@ -79,18 +79,18 @@ namespace cocos2d{
7979
}
8080
void CCLabelTTF::setString(const char *label)
8181
{
82-
m_sString = string(label);
82+
m_pString.reset(new std::string(label));
8383

8484
CCTexture2D *texture;
8585
if( CCSize::CCSizeEqualToSize( m_tDimensions, CCSizeZero ) )
8686
{
8787
texture = new CCTexture2D();
88-
texture->initWithString(label, m_sFontName.c_str(), m_fFontSize);
88+
texture->initWithString(label, m_pFontName->c_str(), m_fFontSize);
8989
}
9090
else
9191
{
9292
texture = new CCTexture2D();
93-
texture->initWithString(label, m_tDimensions, m_eAlignment, m_sFontName.c_str(), m_fFontSize);
93+
texture->initWithString(label, m_tDimensions, m_eAlignment, m_pFontName->c_str(), m_fFontSize);
9494
}
9595
this->setTexture(texture);
9696
texture->release();
@@ -102,13 +102,13 @@ namespace cocos2d{
102102

103103
const char* CCLabelTTF::getString(void)
104104
{
105-
return m_sString.c_str();
105+
return m_pString->c_str();
106106
}
107107

108108
char * CCLabelTTF::description()
109109
{
110110
char *ret = new char[100] ;
111-
sprintf(ret, "<CCLabelTTF | FontName = %s, FontSize = %.1f>", m_sFontName.c_str(), m_fFontSize);
111+
sprintf(ret, "<CCLabelTTF | FontName = %s, FontSize = %.1f>", m_pFontName->c_str(), m_fFontSize);
112112
return ret;
113113
}
114114
}// namespace cocos2d

cocos2dx/platform/CCCommon.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct ccxNewArrayDeleter { template< class TPTR > void operator()(TPTR p) { d
5555
@brief A simple scoped pointer.
5656
*/
5757
template < class T, class D = ccxNewDeleter >
58-
class ccxScopedPtr // noncopyable
58+
class CC_DLL ccxScopedPtr // noncopyable
5959
: private D
6060
{
6161
public:

0 commit comments

Comments
 (0)