0

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.

Jack
  • 950
  • 2
  • 17
  • 36
  • What do you mean "in the header"? And an "initialize method"? Don't post one liners, you need a [mcve] if you are to receive any help. – StoryTeller - Unslander Monica Aug 28 '17 at 05:56
  • Apologies, I've updates with completed code samples – Jack Aug 28 '17 at 06:01
  • Please provide also implementation of `Sprite` class. – vasek Aug 28 '17 at 06:07
  • "Minimal, Complete, and Verifiable example" means something we can copy/paste right into an editor and it will compile with no modifications. If the make_unique call is failing then clearly the exception is occurring in the Sol::Library::Graphics::Sprite constructor, for which we don't have source code. – Richard Hodges Aug 28 '17 at 06:07
  • Sorry, I didn't think the implementation was important because I don't do anything with the constructor in it. I'm very new to C++, I've mostly worked with C# and web languages. – Jack Aug 28 '17 at 06:10
  • Your implementation is missing constructor at all. If you don't do anything in constructor or destructor, just don't declare them in header so that defaults will be generated. – vasek Aug 28 '17 at 06:12
  • I had that initially, but I thought not declaring might be part of the problem. In any case I've removed it, but it didn't help. – Jack Aug 28 '17 at 06:13
  • Well then try to focus your attention to your class members. Are you sure construction of `ComPtr m_tex` works fine? What about COM initialization? See [this](https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/windows/codesnippet/CPP/how-to-create-a-classic-com-component-using-wrl_7.cpp). – vasek Aug 28 '17 at 06:37
  • std::unique_ptr as (non const) reference? That stinks. –  Aug 28 '17 at 06:40
  • I'm fairly certain, as this same code ran fine when it was in a different class, all I did was refactor. And I can't use the unique_ptr as a const because it needs to be set after the directx device is set up – Jack Aug 28 '17 at 13:27
  • You don't have an implementation of the ``Sprite`` ctor. BTW, passing ``std::unique_ptr`` as an input parameter is a bit wordy, so I generally prefer to pass raw pointers where ownership remains clear. Also, in cpp files you generally don't need explicit namespace scopes around classes as you can just include the header which does this and rely on ``using namespace`` statements to resolve things--with the exception of free functions that need explicit scope. Take a look at the DirectXTK source for examples of this style. – Chuck Walbourn Aug 31 '17 at 10:22
  • The ``read access exception`` implies to me that you didn't actually initialize the ``MainMenu`` class instance. – Chuck Walbourn Aug 31 '17 at 10:25

0 Answers0