Skip to content
Permalink
Browse files

Use 'Scope's instead of raw pointers (mem leak)

  • Loading branch information...
IshoAntar authored and LovelySanta committed Oct 5, 2019
1 parent 32c0f56 commit bee34b9d9ad1ef0ee8c67b86059e1daceb3f9b24
@@ -79,8 +79,18 @@ namespace Hazel {

template<typename T>
using Scope = std::unique_ptr<T>;
template<typename T, typename ... Args>
constexpr Scope<T> CreateScope(Args&& ... args)
{
return std::make_unique<T>(std::forward<Args>(args)...);
}

template<typename T>
using Ref = std::shared_ptr<T>;
template<typename T, typename ... Args>
constexpr Ref<T> CreateRef(Args&& ... args)
{
return std::make_shared<T>(std::forward<Args>(args)...);
}

}
@@ -26,7 +26,6 @@ namespace Hazel {
virtual float GetMouseXImpl() = 0;
virtual float GetMouseYImpl() = 0;
private:
static Input* s_Instance;
static Scope<Input> s_Instance;
};

}
@@ -5,6 +5,6 @@

namespace Hazel {

RendererAPI* RenderCommand::s_RendererAPI = new OpenGLRendererAPI;
Scope<RendererAPI> RenderCommand::s_RendererAPI = CreateScope<OpenGLRendererAPI>();

}
@@ -32,7 +32,7 @@ namespace Hazel {
s_RendererAPI->DrawIndexed(vertexArray);
}
private:
static RendererAPI* s_RendererAPI;
static Scope<RendererAPI> s_RendererAPI;
};

}
@@ -5,7 +5,7 @@

namespace Hazel {

Renderer::SceneData* Renderer::s_SceneData = new Renderer::SceneData;
Scope<Renderer::SceneData> Renderer::s_SceneData = CreateScope<Renderer::SceneData>();

void Renderer::Init()
{
@@ -25,8 +25,6 @@ namespace Hazel {
glm::mat4 ViewProjectionMatrix;
};

static SceneData* s_SceneData;
static Scope<SceneData> s_SceneData;
};


}
@@ -42,21 +42,21 @@ namespace Hazel {
Add(name, shader);
}

Hazel::Ref<Hazel::Shader> ShaderLibrary::Load(const std::string& filepath)
Ref<Shader> ShaderLibrary::Load(const std::string& filepath)
{
auto shader = Shader::Create(filepath);
Add(shader);
return shader;
}

Hazel::Ref<Hazel::Shader> ShaderLibrary::Load(const std::string& name, const std::string& filepath)
Ref<Shader> ShaderLibrary::Load(const std::string& name, const std::string& filepath)
{
auto shader = Shader::Create(filepath);
Add(name, shader);
return shader;
}

Hazel::Ref<Hazel::Shader> ShaderLibrary::Get(const std::string& name)
Ref<Shader> ShaderLibrary::Get(const std::string& name)
{
HZ_CORE_ASSERT(Exists(name), "Shader not found!");
return m_Shaders[name];
@@ -6,7 +6,7 @@

namespace Hazel {

Input* Input::s_Instance = new WindowsInput();
Scope<Input> Input::s_Instance = CreateScope<WindowsInput>();

bool WindowsInput::IsKeyPressedImpl(int keycode)
{
@@ -50,7 +50,8 @@ namespace Hazel {

m_Window = glfwCreateWindow((int)props.Width, (int)props.Height, m_Data.Title.c_str(), nullptr, nullptr);

m_Context = new OpenGLContext(m_Window);
m_Context = CreateScope<OpenGLContext>(m_Window);

m_Context->Init();

glfwSetWindowUserPointer(m_Window, &m_Data);
@@ -29,7 +29,7 @@ namespace Hazel {
virtual void Shutdown();
private:
GLFWwindow* m_Window;
GraphicsContext* m_Context;
Scope<GraphicsContext> m_Context;

struct WindowData
{

0 comments on commit bee34b9

Please sign in to comment.
You can’t perform that action at this time.