Some more information after a lot of testing. I'll start by showing how the window is first created and then when the DirectX stuff is called. The DirectX stuff is initialised after the window is created as the SwapChain in DirectX needs a window handle to work.
This code is in the main project:
Code:
MyRegisterClass(hInstance);
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_BASIC_RENDER));
MSG msg;
try
{
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow, pD3DInterface))
{
return FALSE;
}
(*pD3DInterface)->InitDirectX();
I've left other parts of the code in as there isn't too much of it there. Note the window class is registered, and then InitInstance() is called. This is where the CreateWindowW function is contained. I'll show it below:
Code:
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, D3DInterface** pD3DInterface)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW( szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
pD3DInterface);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
Now what's interesting to note is that several messages actually go to the window message handler before the CreateWindowW() function is even complete. They're as follows (I tracked them all):
Code:
36 - WM_GETMINMAXINFO
129 - WM_NCCREATE
131 - WM_NCCALCSIZE
147 - UNKNOWN
148 - UNKNOWN
148 - UNKNOWN
1 - WM_CREATE
// finally the message sent by the ShowWindow() function turns up
24 - WM_SHOWWINDOW
So if I called anything related to the DirectX stuff in either of the WM_NCCREATE or WM_CREATE message blocks in the message handler it would fail as the code had not proceeded out of the CreateWindowW function yet and thus had not had chance to called the (*pD3DInterface)->InitDirectX(); line.
Ok well that's a start. I was originally under the assumption I had to call the Dialog Box to enable to program to end properly. As it turns out this isn't true. What I do need to do however is call any function in the rendering class that has a nested call to the IDXGIFactory4 interface which is declared in the rendering class like so:
Code:
Microsoft::WRL::ComPtr<IDXGIFactory4> mdxgiFactory;
Any valid call to any of this interface's methods cause the program to shut down correctly with no hang ups. Here's the window message handler again with some slight updates and only the relevant parts:
Code:
static HINSTANCE hInstance;
static D3DRenderer** __pD3DRenderer;
static D3DRenderer** _pD3DRenderer;
switch (message)
{
case WM_NCCREATE:
{
CREATESTRUCT* pCreate = reinterpret_cast<CREATESTRUCT*>(lParam);
__pD3DRenderer = reinterpret_cast<D3DRenderer**>(pCreate->lpCreateParams);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)__pD3DRenderer);
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
// MSDN states this message must return TRUE if handled (checked) - 11/3/24
return TRUE;
}
case WM_CREATE:
{
OutputDebugStringW(L"WM_CREATE entered \n\n");
LONG_PTR ptr = GetWindowLongPtr(hWnd, GWLP_USERDATA);
_pD3DRenderer = reinterpret_cast<D3DRenderer**>(ptr);
(*_pD3DRenderer)->mhMainWnd = hWnd;
// MSDN states this message must return 0 (not TRUE or FALSE) if handled (checked) - 11/3/24
return 0;
}
.... further down
case WM_DESTROY:
{
//this line is what saves it
(*_pD3DRenderer)->LogAdapter();
OutputDebugStringW(L"WM_DESTROY entered \n\n");
PostQuitMessage(0);
}
So the WM_NCCREATE block sets up the pointer, the WM_CREATE block gets the pointer (I wanted the two operations done in different message blocks just in case) and any command to close the window has to go through the WM_DESTROY block, which ultimately calls a method in the IDXGIFactory4 interface.
And that means no more issues on shutdown.
So in short, any valid call in any appropriate part of the window message handler, to any of the IDXGIFactory4 interface methods, means I can close the window with no unhandled exceptions or memory issues. If this call is omitted I get the errors shown above.
This works....but I have absolutely no idea why
Thanks