fixing some bad memory usage

This commit is contained in:
Kbz-8
2024-03-25 13:57:20 +01:00
parent 57c5ae5415
commit be52578d37
35 changed files with 1294 additions and 1298 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2024/02/25 07:52:04 by maldavid ### ########.fr */
/* Updated: 2024/03/24 14:39:23 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -89,10 +89,9 @@ namespace mlx::core
core::error::report(e_kind::error, "invalid image ptr (NULL)");
return;
}
else if(std::find_if(_textures.begin(), _textures.end(), [=](const Texture& texture)
{
return &texture == ptr;
}) == _textures.end())
auto it = std::find_if(_textures.begin(), _textures.end(), [=](const Texture& texture) { return &texture == ptr; });
if(it == _textures.end())
{
core::error::report(e_kind::error, "invalid image ptr");
return;
@@ -102,6 +101,9 @@ namespace mlx::core
core::error::report(e_kind::error, "trying to destroy a texture that has already been destroyed");
else
texture->destroy();
for(auto& gs : _graphics)
gs->tryEraseTextureFromManager(texture);
_textures.erase(it);
}
Application::~Application()

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */
/* Updated: 2024/01/11 15:47:05 by maldavid ### ########.fr */
/* Updated: 2024/03/24 14:43:09 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,6 @@
#define __MLX_GRAPHICS__
#include <memory>
#include <unordered_set>
#include <filesystem>
#include <glm/glm.hpp>
@@ -33,7 +32,7 @@
namespace mlx
{
class GraphicsSupport : public non_copyable
class GraphicsSupport : public NonCopyable
{
public:
GraphicsSupport(std::size_t w, std::size_t h, Texture* render_target, int id);
@@ -49,6 +48,7 @@ namespace mlx
inline void stringPut(int x, int y, std::uint32_t color, std::string str);
inline void texturePut(Texture* texture, int x, int y);
inline void loadFont(const std::filesystem::path& filepath, float scale);
inline void tryEraseTextureFromManager(Texture* texture) noexcept;
inline bool hasWindow() const noexcept { return _has_window; }

View File

@@ -11,7 +11,6 @@
/* ************************************************************************** */
#include <core/graphics.h>
#include <iostream>
namespace mlx
{
@@ -64,4 +63,17 @@ namespace mlx
MLX_PROFILE_FUNCTION();
_text_manager.loadFont(*_renderer, filepath, scale);
}
void GraphicsSupport::tryEraseTextureFromManager(Texture* texture) noexcept
{
MLX_PROFILE_FUNCTION();
for(auto it = _drawlist.begin(); it != _drawlist.end();)
{
if(_texture_manager.isTextureKnown(texture))
it = _drawlist.erase(it);
else
++it;
}
_texture_manager.eraseTextures(texture);
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 13:35:45 by maldavid #+# #+# */
/* Updated: 2024/01/10 18:36:46 by maldavid ### ########.fr */
/* Updated: 2024/03/24 14:41:27 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,10 +19,10 @@
#include <string>
#include <thread>
#include <mutex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <unordered_map>
#include <sstream>
#include <iomanip>
namespace mlx
{
@@ -115,17 +115,17 @@ namespace mlx
{
ChangeResult<N> result = {};
std::size_t srcIndex = 0;
std::size_t dstIndex = 0;
while(srcIndex < N)
std::size_t src_index = 0;
std::size_t dst_index = 0;
while(src_index < N)
{
std::size_t matchIndex = 0;
while(matchIndex < K - 1 && srcIndex + matchIndex < N - 1 && expr[srcIndex + matchIndex] == remove[matchIndex])
matchIndex++;
if(matchIndex == K - 1)
srcIndex += matchIndex;
result.data[dstIndex++] = expr[srcIndex] == '"' ? '\'' : expr[srcIndex];
srcIndex++;
std::size_t match_index = 0;
while(match_index < K - 1 && src_index + match_index < N - 1 && expr[src_index + match_index] == remove[match_index])
match_index++;
if(match_index == K - 1)
src_index += match_index;
result.data[dst_index++] = expr[src_index] == '"' ? '\'' : expr[src_index];
src_index++;
}
return result;
}