C++ std::make_unique<> Assign to Class Property

218 views Asked by At

I have what should be a simple operation, but I'm having difficulty figuring out what is wrong.

I have a private property in my header file like so:

#pragma once
#include "Sprite.h"
#include "MainMenuContent.h"

class MainMenu
{
public:
    MainMenu();

    void Initialize(ID3D11Device* device, ID3D11DeviceContext* context, int width, int height,
                    std::unique_ptr<Sol::Library::Content::UI::MainMenuContent> &content);

    void Draw();

    void OnDeviceLost();

    ~MainMenu();

private:
    //...some other properties
    std::unique_ptr<Sol::Library::Graphics::Sprite> m_title;
};

I'm trying to assign it in an initialize method like so:

    void MainMenu::Initialize(ID3D11Device* device, ID3D11DeviceContext* context, int width, int height,
                          std::unique_ptr<MainMenuContent> &content)
{    
    //...some other code that seems to work ok

    m_title = std::make_unique<Sol::Library::Graphics::Sprite>(); //<--this throws the error
}

But I get an error:

Exception thrown: read access violation.
**std::_Unique_ptr_base<Sol::Library::Graphics::Sprite,std::default_delete<Sol::Library::Graphics::Sprite> >::_Myptr**(...) returned 0x8. occurred

For reference, there is nothing in the constructor of my Sprite class, it's declared in it's own header file:

#pragma once

namespace Sol
{
    namespace Library
    {
        namespace Graphics
        {
            class Sprite
            {
            public:
                Sprite();

                virtual void Update();
                void Draw(std::unique_ptr<DirectX::SpriteBatch> &sb);

                void OnDeviceLost();

                ~Sprite();

            private:
                Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>    m_tex;
                DirectX::SimpleMath::Vector2                        m_pos;
                DirectX::SimpleMath::Vector2                        m_ori;
            };
        }
    }
}

And the definition of the Sprite class:

#include "pch.h"
#include "Sprite.h"

using namespace std;
using namespace DirectX;

namespace Sol
{
    namespace Library
    {
        namespace Graphics
        {
            void Sprite::Update()
            {

            }

            void Sprite::Draw(unique_ptr<SpriteBatch> &sb)
            {
                sb->Draw(m_tex.Get(), m_pos, nullptr, Colors::White, 0.f, m_ori);
            }

            void Sprite::OnDeviceLost()
            {
                m_tex.Reset();
            }


            Sprite::~Sprite()
            {
            }
        }
    }
}

Any help you guys could provide would be really appreciated.

0

There are 0 answers