'std::shared_ptr gets filled with random data after initialized in a static library

I'm working on a game engine (I wanted to try something new and I've been experimenting with OpenGL a lot recently) and I have followed TheCherno's tutorial for the beginning (since I didn't where and how to start) and then started to continue on my own. Basically I have 2 projects, one is the main engine (Copper) which builds as a static library (.lib) and then second project which is the .exe (Sandbox).

The problem is that I have a std::shared_ptr variable in my Application class (which is in the Copper lib and contains things such as Initialization code, Main Game loop etc) that is Initialized as one of the first things in the program. Then it goes to the Sandbox to return the new Initialized Application and then it Runs the Main Game loop in the Application, however after it gets back to the Application the std::shared_ptr is filled with completely random values.

I don't even know what's really happening so I didn't really try anything else then to Debug the problem and look through stackoverflow, msdn and all other forums and I couldn't a single answer that would help me.

Here is some code.

EntryPoint.h: Here the program starts running

#pragma once

#ifdef CU_PLATFORM_WINDOWS

extern Copper::Application* Copper::CreateApplication();

int main(int argc, char** argv) {

    Copper::Log::Initialize();
    LogNormal("Logger Initialized succesffully!");

    auto app = Copper::CreateApplication(); //Here it jumps to Sandbox to create new Application
    app->Run();
    delete app;

}

#endif

SandboxApp.cpp: this is the main file for the Sandbox.exe

#include <Copper.h>

class ExampleLayer : public Copper::Layer {

public:
    ExampleLayer() : Layer("Example") {}

    void OnUpdate() override {

        //

    }

    void OnEvent(Copper::Event& event) override {}


};

class Sandbox : public Copper::Application {

public:
    Sandbox() {

        PushLayer(new ExampleLayer());

    }

    ~Sandbox() {}

};

Copper::Application* Copper::CreateApplication() { //This is the function that got called in EntryPoint.h

    return new Sandbox();

}

Application.h: This is the main Application class that SandboxApp derives from

#pragma once

#include "Core.h"

#include "Copper/Events/Event.h"
#include "Copper/Events/ApplicationEvent.h"
#include "Window.h"
#include "Input.h"
#include "LayerStack.h"
#include "Copper/UI/ImGuiLayer.h"

#include "Copper/Renderer/RendererCommand.h"

#include "Copper/Renderer/Shader.h"
#include "Copper/Renderer/VertexArray.h"
#include "Copper/Renderer/Buffer.h"
#include "Copper/Renderer/Texture.h"

#include "Copper/Scene/Object.h"
#include "Copper/Scene/Components.h"

namespace Copper {

    class COPPER_API Application {

    public:
        Application();

        void Run();

        void OnEvent(Event& e);

        void PushLayer(Layer* layer);
        void PushOverlay(Layer* overlay);

        inline static Application& Get() { return *instance; }
        inline Window& GetWindow() { return *window; }
        inline void* GetGLFWWindow() { return window->GetNativeWindow(); }

    private:
        bool OnWindowClose(WindowCloseEvent event);

        static Application* instance;

        std::unique_ptr<Window> window;
        bool running = true;

        ImGuiLayer* imguiLayer;
        LayerStack layerStack;

        Ref<Shader> shader;
    
        Ref<VertexArray> vao;
        Ref<VertexBuffer> vbo;
        Ref<IndexBuffer> ibo;

        Ref<Texture2D> texture;

        Ref<Object> obj; //This is the variable that gets filled with random stuff
    };

    Application* CreateApplication();

}

Nothing in here is really important to this problem, but the most important function here is the Constructor for Application where too nothing is important except the end of the function where I initialize obj.

    Scene scene;
    obj = scene.Instantiate();

    Transform* t = obj->AddComponent<Transform>();
    Mesh* m = obj->AddComponent<Mesh>();

    m->SetVertexArray(vao);
    m->SetTexture(texture);

Here the Object gets initialized normally, it's values look like this: Initialized Object, success

After this is done the program goes back to SanboxApp constructor:

Sandbox() {

    PushLayer(new ExampleLayer());

}

here everything works, but then it goes to the PushLayer function in Application:

void Application::PushLayer(Layer* layer) {

    layerStack.PushLayer(layer);
    layer->OnAttach();

}

As soon as the program enters this function the Object's scene pointer is filled with random data, at the first line of the function the Object variable looks like this: Object filled with random data

As you can see the Handle variable in Object is alright but the scene pointer is broken, the Registry is filled with vectors with completely random data, the worst part is that as soon as the program progresses to the next line of code in the function the Object variable looks completely different again.

I know there is probably some information missing but I don't know what else to add, genuinely this program is so complicated in structure that I probably missed something too. If you need anything to be added comment it here and I will add it here or respond to the comment. I don't know what really is happening only thing I know is that there is something wrong with the Scene.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source