cocos2d-x / cocos2dx-win8

102 stars 66 forks source link

CCDXParticleSystemQuad - buffer overflow and potential leak fixes #10

Closed mingulov closed 12 years ago

mingulov commented 12 years ago

Briefly, due to the buffer overflow at CCDXParticleSystemQuad - it was impossible to use more than 1 CCDXParticleSystemQuad object in a lot of cases.

CCDXParticleSystemQuad is used as a static, so it is initialized just once for multiple CCParticleSystemQuads. As m_uMaxTotalParticles was almost useless - some internal buffers have been initialized just once (by CCDXParticleSystemQuad::initVertexAndIndexBuffer), so there was a buffer overflow in case if a small buffer (e.g. for 20 particles) has been initialized originally, and later another CCParticleQuad with more particles (but less than 100, for example 50) tried to be handled - CCDXParticleSystemQuad::RenderVertexBuffer overflow buffer by rewriting the originally allocated memory with more data.

Consequences - random crashes (if DirectX driver has no protection against buffer overflow), display flickering and so on. E.g. it was possible that some CCParticleSystemQuads were not shown at all, some are shown particularly (as less indices were provided than needed).

Such behaviour is fixed by this pull request. m_uMaxTotalParticles is used now as the currently allocated buffer size, and buffers are dynamically reallocated when it is needed. No indexes are lost, no memory corruption.

3 commits: 1) Test is created for the bug. Should be started from tests app: BugsTests - Particle Buffer Overflow before any other particles are created. 2) Fix for the buffer overflow itself. 3) Some potential memory leaks are fixed.

lihex commented 12 years ago

thank you mingulov, you did a nice work!