Cannot create std::list on the stack

187 views Asked by At

I've been racking my head with this one all night. I'm working with a Windows Application and for whatever reason, I cannot create a instance of std::list on the stack. It causes CreateWindow() to fail, and it's not telling me anything useful.

My Window code is pretty standard, nothing out of the ordinary aside from a few things for my program.

#include <windows.h>
#include <string.h>
#include <tchar.h>
#include <sstream>

#include "GameProcessor.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
               HINSTANCE hPrevInstance,
               LPSTR lpCmdLine,
               int nCmdShow)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style          = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = WndProc;
wcex.cbClsExtra     = 0;
wcex.cbWndExtra     = 0;
wcex.hInstance      = hInstance;
wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName   = NULL;
wcex.lpszClassName  =(LPCWSTR) "mainWin";
wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

if (!RegisterClassEx(&wcex))
{
    MessageBox(NULL,
        _T("Call to RegisterClassEx failed!"),
        _T("Win32 Guided Tour"),
        NULL);

    return 1;
}

static TCHAR szWindowClass[] = _T("win32app");
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
    (LPCWSTR) "mainWin",
    szTitle,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT,
    WINDOW_WIDTH, WINDOW_HEIGHT,
    NULL,
    NULL,
    hInstance,
    NULL
);



if (!hWnd)
{
    MessageBox(NULL,
        _T("Call to CreateWindow failed!"),
        _T("Win32 Guided Tour"),
        NULL);

    return 1;
}

// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

/*===========================================
    initialize game stuff here
=============================================*/
OIS::ParamList pl;
std::ostringstream wnd;
wnd << (size_t)hWnd;
pl.insert(std::make_pair( std::string("WINDOW"), wnd.str() ));
CGameProcessor * gameProcessor = new CGameProcessor(pl);

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
    gameProcessor->run();
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

/*=============================================
    CLEAN THAT SHIT UP
===============================================*/
delete gameProcessor;
gameProcessor = nullptr;

return (int) msg.wParam;

}

This is where I tried creating the std::list

#ifndef _GAMEPROCESSOR_H_
#define _GAMEPROCESSOR_H_

#include <fstream>
#include <iostream>
#include <map>
#include "EventProcessor.h"


#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define ENTITYCFG "entities\\config.cfg"

class CGameProcessor
{
public:
void run();
CGameProcessor(OIS::ParamList pl);


private:

//no implementation -- do not use
CGameProcessor(const CGameProcessor &);
CGameProcessor & operator= (const CGameProcessor &);

static CGameProcessor * _singleton;
CEventProcessor * _eventProcessor;
std::list<int> _foo;


};

#endif

I've tried moving it around to other classes, it seems to bomb out wherever I put it. If I use std::list<int> * instead, and allocate it in the ctor, there are no problems. I REALLY don't want to use a pointer for this though, that's just silly. std::vector also works fine. If I can't get around this, I may just end up using that instead. Has anyone seen anything like this before?

1

There are 1 answers

2
hmjd On BEST ANSWER

This is an error:

HWND hWnd = CreateWindow(
    (LPCWSTR) "mainWin"

as it is casting a char string literal to a wchar_t*. Use _T() macro as is done elsewhere or use a wide string literal (L"mainWin"). There is an identical error earlier in the code also when assigning to wcex.lpszClassName.

If CreateWindow() fails use GetLastError() to determine the reason for the failure.