From 8ca09f7c2a27ddc905ebf655c460ed9a8fe25a7c Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Sun, 15 May 2016 12:30:30 +1000 Subject: [PATCH] Fix crash due to reallocation of shared indices memory --- cocos/renderer/CCQuadCommand.cpp | 12 +++++++++--- cocos/renderer/CCQuadCommand.h | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cocos/renderer/CCQuadCommand.cpp b/cocos/renderer/CCQuadCommand.cpp index 9467eb2c28..b7762e34d6 100644 --- a/cocos/renderer/CCQuadCommand.cpp +++ b/cocos/renderer/CCQuadCommand.cpp @@ -39,13 +39,18 @@ NS_CC_BEGIN int QuadCommand::__indexCapacity = -1; GLushort* QuadCommand::__indices = nullptr; -QuadCommand::QuadCommand() -: _indexSize(-1) +QuadCommand::QuadCommand(): +_indexSize(-1), +_ownedIndices() { } QuadCommand::~QuadCommand() { + for (auto& indices : _ownedIndices) + { + CC_SAFE_DELETE_ARRAY(indices); + } } void QuadCommand::init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, const BlendFunc& blendType, V3F_C4B_T2F_Quad* quads, ssize_t quadCount, @@ -70,7 +75,8 @@ void QuadCommand::reIndex(int indicesCount) if (indicesCount > __indexCapacity) { CCLOG("cocos2d: QuadCommand: resizing index size from [%d] to [%d]", __indexCapacity, indicesCount); - __indices = (GLushort*) realloc(__indices, indicesCount * sizeof(__indices[0])); + _ownedIndices.push_back(__indices); + __indices = new (std::nothrow) GLushort[indicesCount]; __indexCapacity = indicesCount; } diff --git a/cocos/renderer/CCQuadCommand.h b/cocos/renderer/CCQuadCommand.h index e54674a238..7bec874e18 100644 --- a/cocos/renderer/CCQuadCommand.h +++ b/cocos/renderer/CCQuadCommand.h @@ -25,6 +25,8 @@ #ifndef _CC_QUADCOMMAND_H_ #define _CC_QUADCOMMAND_H_ +#include + #include "renderer/CCTrianglesCommand.h" #include "renderer/CCGLProgramState.h" @@ -69,6 +71,7 @@ protected: void reIndex(int indices); int _indexSize; + std::vector _ownedIndices; // shared across all instances static int __indexCapacity;