Skip to content

Commit 5ddf8fb

Browse files
monkey256minggo
authored andcommitted
Fix a crash bug when the UserDefault.xml file is empty. (cocos2d#18626)
* Fix a crash bug when the UserDefault.xml file is empty. * Fix a crash bug when play multiple timeline animations. * Fix a crash bug when loading .plist files. * Fix code style.
1 parent 7e27c25 commit 5ddf8fb

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

cocos/base/CCUserDefault.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,41 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLEle
5757

5858
do
5959
{
60-
tinyxml2::XMLDocument* xmlDoc = new (std::nothrow) tinyxml2::XMLDocument();
60+
tinyxml2::XMLDocument* xmlDoc = new (std::nothrow) tinyxml2::XMLDocument();
6161
*doc = xmlDoc;
62+
*rootNode = nullptr;
6263

6364
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
6465

65-
if (xmlBuffer.empty())
66+
if (!xmlBuffer.empty())
6667
{
67-
CCLOG("can not read xml file");
68-
break;
68+
xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
69+
70+
// get root node
71+
*rootNode = xmlDoc->RootElement();
6972
}
70-
xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
7173

72-
// get root node
73-
*rootNode = xmlDoc->RootElement();
7474
if (nullptr == *rootNode)
7575
{
76-
CCLOG("read root node error");
77-
break;
76+
// try to insert xml declaration
77+
if (!xmlDoc->FirstChild())
78+
{
79+
tinyxml2::XMLDeclaration *xmlDeclaration = xmlDoc->NewDeclaration(nullptr);
80+
if (nullptr != xmlDeclaration)
81+
{
82+
xmlDoc->LinkEndChild(xmlDeclaration);
83+
}
84+
}
85+
86+
// create root element
87+
tinyxml2::XMLElement *rootEle = xmlDoc->NewElement(USERDEFAULT_ROOT_NAME);
88+
if (nullptr == rootEle)
89+
break;
90+
91+
xmlDoc->LinkEndChild(rootEle);
92+
*rootNode = rootEle;
7893
}
94+
7995
// find the node
8096
curNode = (*rootNode)->FirstChildElement();
8197
while (nullptr != curNode)
@@ -95,7 +111,7 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLEle
95111

96112
static void setValueForKey(const char* pKey, const char* pValue)
97113
{
98-
tinyxml2::XMLElement* rootNode;
114+
tinyxml2::XMLElement* rootNode;
99115
tinyxml2::XMLDocument* doc;
100116
tinyxml2::XMLElement* node;
101117
// check the params

cocos/editor-support/cocostudio/ActionTimeline/CCFrame.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ TextureFrame* TextureFrame::create()
149149
}
150150

151151
TextureFrame::TextureFrame()
152-
: _textureName("")
152+
: _sprite(nullptr)
153+
, _textureName("")
153154
{
154155
}
155156

cocos/platform/CCFileUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class DictMaker : public SAXDelegator
8686
public:
8787
DictMaker()
8888
: _resultType(SAX_RESULT_NONE)
89+
, _state(SAX_NONE)
8990
{
9091
}
9192

0 commit comments

Comments
 (0)