Merge pull request #11091 from super626/v3

fix memory leak
This commit is contained in:
minggo 2015-03-25 16:16:28 +08:00
commit cb07aa4ae2
3 changed files with 53 additions and 35 deletions

View File

@ -375,16 +375,17 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str()); CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
return false; return false;
} }
MeshData* meshData = nullptr;
for(unsigned int i = 0; i < meshSize ; i++ ) for(unsigned int i = 0; i < meshSize ; i++ )
{ {
MeshData* meshData = new (std::nothrow) MeshData();
unsigned int attribSize=0; unsigned int attribSize=0;
// read mesh data // read mesh data
if (_binaryReader.read(&attribSize, 4, 1) != 1 || attribSize < 1) if (_binaryReader.read(&attribSize, 4, 1) != 1 || attribSize < 1)
{ {
CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str()); CCLOG("warning: Failed to read meshdata: attribCount '%s'.", _path.c_str());
return false; goto FAILED;
} }
meshData = new (std::nothrow) MeshData();
meshData->attribCount = attribSize; meshData->attribCount = attribSize;
meshData->attribs.resize(meshData->attribCount); meshData->attribs.resize(meshData->attribCount);
for (ssize_t j = 0; j < meshData->attribCount; j++) for (ssize_t j = 0; j < meshData->attribCount; j++)
@ -394,7 +395,7 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
if (_binaryReader.read(&vSize, 4, 1) != 1) if (_binaryReader.read(&vSize, 4, 1) != 1)
{ {
CCLOG("warning: Failed to read meshdata: usage or size '%s'.", _path.c_str()); CCLOG("warning: Failed to read meshdata: usage or size '%s'.", _path.c_str());
return false; goto FAILED;
} }
std::string type = _binaryReader.readString(); std::string type = _binaryReader.readString();
attribute=_binaryReader.readString(); attribute=_binaryReader.readString();
@ -408,14 +409,14 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
if (_binaryReader.read(&vertexSizeInFloat, 4, 1) != 1 || vertexSizeInFloat == 0) if (_binaryReader.read(&vertexSizeInFloat, 4, 1) != 1 || vertexSizeInFloat == 0)
{ {
CCLOG("warning: Failed to read meshdata: vertexSizeInFloat '%s'.", _path.c_str()); CCLOG("warning: Failed to read meshdata: vertexSizeInFloat '%s'.", _path.c_str());
return false; goto FAILED;
} }
meshData->vertex.resize(vertexSizeInFloat); meshData->vertex.resize(vertexSizeInFloat);
if (_binaryReader.read(&meshData->vertex[0], 4, vertexSizeInFloat) != vertexSizeInFloat) if (_binaryReader.read(&meshData->vertex[0], 4, vertexSizeInFloat) != vertexSizeInFloat)
{ {
CCLOG("warning: Failed to read meshdata: vertex element '%s'.", _path.c_str()); CCLOG("warning: Failed to read meshdata: vertex element '%s'.", _path.c_str());
return false; goto FAILED;
} }
// Read index data // Read index data
@ -431,13 +432,13 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
if (_binaryReader.read(&nIndexCount, 4, 1) != 1) if (_binaryReader.read(&nIndexCount, 4, 1) != 1)
{ {
CCLOG("warning: Failed to read meshdata: nIndexCount '%s'.", _path.c_str()); CCLOG("warning: Failed to read meshdata: nIndexCount '%s'.", _path.c_str());
return false; goto FAILED;
} }
indexArray.resize(nIndexCount); indexArray.resize(nIndexCount);
if (_binaryReader.read(&indexArray[0], 2, nIndexCount) != nIndexCount) if (_binaryReader.read(&indexArray[0], 2, nIndexCount) != nIndexCount)
{ {
CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str()); CCLOG("warning: Failed to read meshdata: indices '%s'.", _path.c_str());
return false; goto FAILED;
} }
meshData->subMeshIndices.push_back(indexArray); meshData->subMeshIndices.push_back(indexArray);
meshData->numIndex = (int)meshData->subMeshIndices.size(); meshData->numIndex = (int)meshData->subMeshIndices.size();
@ -449,7 +450,7 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
if (_binaryReader.read(aabb, 4, 6) != 6) if (_binaryReader.read(aabb, 4, 6) != 6)
{ {
CCLOG("warning: Failed to read meshdata: aabb '%s'.", _path.c_str()); CCLOG("warning: Failed to read meshdata: aabb '%s'.", _path.c_str());
return false; goto FAILED;
} }
meshData->subMeshAABB.push_back(AABB(Vec3(aabb[0], aabb[1], aabb[2]), Vec3(aabb[3], aabb[4], aabb[5]))); meshData->subMeshAABB.push_back(AABB(Vec3(aabb[0], aabb[1], aabb[2]), Vec3(aabb[3], aabb[4], aabb[5])));
} }
@ -461,6 +462,16 @@ bool Bundle3D::loadMeshDatasBinary(MeshDatas& meshdatas)
meshdatas.meshDatas.push_back(meshData); meshdatas.meshDatas.push_back(meshData);
} }
return true; return true;
FAILED:
{
CC_SAFE_DELETE(meshData);
for (auto& meshdata : meshdatas.meshDatas) {
delete meshdata;
}
meshdatas.meshDatas.clear();
return false;
}
} }
bool Bundle3D::loadMeshDatasBinary_0_1(MeshDatas& meshdatas) bool Bundle3D::loadMeshDatasBinary_0_1(MeshDatas& meshdatas)
{ {

View File

@ -49,7 +49,25 @@ struct MeshVertexAttrib
}; };
struct ModelData; /** model node data, since 3.3 */
struct ModelData
{
std::string subMeshId;
std::string matrialId;
std::vector<std::string> bones;
std::vector<Mat4> invBindPose;
virtual ~ModelData()
{
resetData();
}
virtual void resetData()
{
bones.clear();
invBindPose.clear();
}
};
/** Node data, since 3.3 */ /** Node data, since 3.3 */
struct NodeData struct NodeData
{ {
@ -71,29 +89,16 @@ struct NodeData
delete it; delete it;
} }
children.clear(); children.clear();
for(auto& modeldata : modelNodeDatas)
{
delete modeldata;
}
modelNodeDatas.clear();
} }
}; };
/** model node data, since 3.3 */
struct ModelData
{
std::string subMeshId;
std::string matrialId;
std::vector<std::string> bones;
std::vector<Mat4> invBindPose;
virtual ~ModelData()
{
resetData();
}
virtual void resetData()
{
bones.clear();
invBindPose.clear();
}
};
/** node datas, since 3.3 */ /** node datas, since 3.3 */
struct NodeDatas struct NodeDatas
{ {

View File

@ -150,14 +150,15 @@ void Sprite3D::afterAsyncLoad(void* param)
} }
Sprite3DCache::getInstance()->addSprite3DData(asyncParam->modlePath, data); Sprite3DCache::getInstance()->addSprite3DData(asyncParam->modlePath, data);
meshdatas = nullptr;
CC_SAFE_DELETE(meshdatas);
materialdatas = nullptr; materialdatas = nullptr;
nodeDatas = nullptr; nodeDatas = nullptr;
} }
} }
delete meshdatas; CC_SAFE_DELETE(meshdatas);
delete materialdatas; CC_SAFE_DELETE(materialdatas);
delete nodeDatas; CC_SAFE_DELETE(nodeDatas);
if (asyncParam->texPath != "") if (asyncParam->texPath != "")
{ {
@ -290,12 +291,13 @@ bool Sprite3D::initWithFile(const std::string &path)
} }
Sprite3DCache::getInstance()->addSprite3DData(path, data); Sprite3DCache::getInstance()->addSprite3DData(path, data);
CC_SAFE_DELETE(meshdatas);
return true; return true;
} }
} }
delete meshdatas; CC_SAFE_DELETE(meshdatas);
delete materialdatas; CC_SAFE_DELETE(materialdatas);
delete nodeDatas; CC_SAFE_DELETE(nodeDatas);
return false; return false;
} }