Crash after graphics exception

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Mikastiv
Posts: 4
Joined: April 10th, 2020, 10:32 pm

Crash after graphics exception

Post by Mikastiv » April 10th, 2020, 10:48 pm

I'm currently learning DirectX through chili's tutorial series. For reference, I'm at episode 14.

So, my problem is that when I put a bad window handle in the device and swap chain creation, a graphics exception is thrown and the MessageBox pops up, which is good. But, when I move my mouse in the window, the whole program crashes. It happens when the queue of mouse events is trying to call pop when the queue is larger than the max size.

Code: Select all

    DXGI_SWAP_CHAIN_DESC sd{};
    /*****/
    sd.OutputWindow = (HWND)45645; // bad window handle
    /*****/
    
    HRESULT hr{};
    // exception thrown here
    GFX_THROW_INFO(D3D11CreateDeviceAndSwapChain(nullptr,
                                                 D3D_DRIVER_TYPE_HARDWARE,
                                                 nullptr,
                                                 swap_create_flags,
                                                 nullptr,
                                                 0,
                                                 D3D11_SDK_VERSION,
                                                 &sd,
                                                 &p_swap,
                                                 &p_device,
                                                 nullptr,
                                                 &p_context));

Code: Select all

// inside trimBuffer function of Mouse class
while (buffer.size() > buffer_size)
{
    // here it crashes everytime with a read access violation: _Pnext was 0x8
    buffer.pop();
}
At first, I thought it was because the event queue wasn't constructed when the exception is thrown, but everything seems good.

Anyone knows what's happening?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Crash after graphics exception

Post by albinopapa » April 11th, 2020, 1:11 am

That is a little weird, but it sounds like what is happening is when the exception is thrown, the mouse object is destroyed, meaning that the event queue isn't valid anymore. When an exception is thrown, all destrcutors are called from objects created inside the matching try{} block since you are leaving that scope.

As to why it's crashing or even trying to handle more events is beyond me right now...though the problem seems a little familiar. I think someone on here also had similar problem.

HW3D exception throwing thing
This is probably the closest to matching, but I'm not sure if the root cause is the same. In their case, the Graphics throwing during construction meant that Game was never fully constructed and therefore the destructor for Game nor MainWindow were never called...this means that the WNDCLASSEX was never Unregistered which mean that the message handler WNDPROC never got released nor the window.

Seems like the better thing to do is have the Window construction be separate from other objects. This way when an exception is thrown in Graphics or Game or App, the fully initialized Window can have it's destructor run.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Mikastiv
Posts: 4
Joined: April 10th, 2020, 10:32 pm

Re: Crash after graphics exception

Post by Mikastiv » April 11th, 2020, 3:58 am

I also thought that the window wasn't initialized, but I checked with the debugger and the window and all its members are in fact constructed. That's why the mouse queue is filled up to the max size and only then it crashes when it tries to pop an element off the queue. And also weird, the window destructor is never called when the exception is thrown. And my App object is inside the try block where the exception is caught. I also tried chili's version at https://github.com/planetchili/hw3d at the tag of episode 14. This also crashed at the same place for me.

Edit: After reviewing, the last line of the window constructor is not fully completed, so maybe the window is not considered fully constructed, thus maybe that's why the destructor is not called.
Last edited by Mikastiv on April 11th, 2020, 4:08 am, edited 1 time in total.

Mikastiv
Posts: 4
Joined: April 10th, 2020, 10:32 pm

Re: Crash after graphics exception

Post by Mikastiv » April 11th, 2020, 4:02 am

Also yes this link: https://forum.planetchili.net/viewtopic ... ion#p30619 is very very similar to my problem.

Mikastiv
Posts: 4
Joined: April 10th, 2020, 10:32 pm

Re: Crash after graphics exception

Post by Mikastiv » April 11th, 2020, 4:16 am

You pretty much got the answer. I fixed it by putting the creation of the Graphics object outside of the class Window's constructor. So now, the Window object is fully constructed and the destructor is called. The only downside is that I now have to call a separate function before starting the game loop to create the graphics object.

Thanks for the help albinopapa :)

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Crash after graphics exception

Post by albinopapa » April 11th, 2020, 4:31 am

Glad it worked out.

This might be why in the 2D framework, chili has the construction of MainWindow in it's own try/catch block.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply