Browse Source

Render: Fix memory leak, use after free :^)

master
Riyyi 3 months ago
parent
commit
3ff8838e01
  1. 7
      src/inferno/render/buffer.cpp
  2. 10
      src/inferno/render/renderer.cpp
  3. 2
      src/inferno/render/renderer.h

7
src/inferno/render/buffer.cpp

@ -424,10 +424,11 @@ void VertexArray::addVertexBuffer(std::shared_ptr<VertexBuffer> vertexBuffer)
index++; index++;
} }
m_vertexBuffers.push_back(std::move(vertexBuffer));
unbind(); unbind();
vertexBuffer->unbind(); vertexBuffer->unbind();
m_vertexBuffers.push_back(std::move(vertexBuffer));
} }
void VertexArray::setIndexBuffer(std::shared_ptr<IndexBuffer> indexBuffer) void VertexArray::setIndexBuffer(std::shared_ptr<IndexBuffer> indexBuffer)
@ -435,10 +436,10 @@ void VertexArray::setIndexBuffer(std::shared_ptr<IndexBuffer> indexBuffer)
bind(); bind();
indexBuffer->bind(); indexBuffer->bind();
m_indexBuffer = std::move(indexBuffer);
unbind(); unbind();
indexBuffer->unbind(); indexBuffer->unbind();
m_indexBuffer = std::move(indexBuffer);
} }
} // namespace Inferno } // namespace Inferno

10
src/inferno/render/renderer.cpp

@ -550,20 +550,20 @@ void Renderer3D::createElementBuffer()
// CPU // CPU
// Create array for storing quads vertices // Create array for storing quads vertices
m_elementBufferBase = new uint32_t[maxElements]; m_elementBufferBase = std::make_unique<uint32_t[]>(maxElements);
m_elementBufferPtr = m_elementBufferBase; m_elementBufferPtr = m_elementBufferBase.get();
// --------------------------------- // ---------------------------------
// GPU // GPU
// Create index buffer // Create index buffer
auto indexBuffer = std::make_shared<IndexBuffer>(m_elementBufferBase, sizeof(uint32_t) * maxElements); auto indexBuffer = std::make_shared<IndexBuffer>(m_elementBufferBase.get(), sizeof(uint32_t) * maxElements);
m_vertexArray->setIndexBuffer(indexBuffer); m_vertexArray->setIndexBuffer(indexBuffer);
} }
void Renderer3D::uploadElementBuffer() void Renderer3D::uploadElementBuffer()
{ {
m_vertexArray->indexBuffer()->uploadData(m_elementBufferBase, m_elementIndex * sizeof(uint32_t)); m_vertexArray->indexBuffer()->uploadData(m_elementBufferBase.get(), m_elementIndex * sizeof(uint32_t));
} }
void Renderer3D::loadShader() void Renderer3D::loadShader()
@ -574,7 +574,7 @@ void Renderer3D::loadShader()
void Renderer3D::startBatch() void Renderer3D::startBatch()
{ {
Renderer<Vertex>::startBatch(); Renderer<Vertex>::startBatch();
m_elementBufferPtr = m_elementBufferBase; m_elementBufferPtr = m_elementBufferBase.get();
} }
// ----------------------------------------- // -----------------------------------------

2
src/inferno/render/renderer.h

@ -222,7 +222,7 @@ private:
private: private:
// CPU element vertices // CPU element vertices
uint32_t* m_elementBufferBase { nullptr }; std::unique_ptr<uint32_t[]> m_elementBufferBase { nullptr };
uint32_t* m_elementBufferPtr { nullptr }; uint32_t* m_elementBufferPtr { nullptr };
}; };

Loading…
Cancel
Save