Weird problems inside and outside visual studio (Component based engine)

Started by
13 comments, last by L. Spiro 9 years, 1 month ago

Hi, this is my first post, first of all english is not my primary language, so I will try my best (and my friend google too)wink.png .

To the problem... I'm trying to create a small game engine (a component based approach), mostly rendering engine at first, but I have a strange error, strange because :

1) when is debugging (VS 2013) it crash, the typical .exe stop working, but when I run the same program directly with the .exe file it works fine.

2) when I'm trying to interact with my classes ( specifically with meshrenderer, material and shader) the program also crash (inside and outside VS)

3) When i'm using only d3d for begin and end scene (not for drawing any gameobject (not with shaders)) everything works fine, but when I include the shader part the program crash. The initialization of the shader result is ok, but the problem is when I add the shader to a material, and that material to a meshrenderer component, and that component to a gameobject (that's why I mean classes interaction)

4) I have created other project with some classes (not a win32 project,but a console application (without any of directx)) and test the member functions that supposedly fail, and works fine (example : create a gameobject , add a component , get that component , etc).

I have a class that is the core of the engine (named Core) , the game setup occurs in a function named Initialize(). In the console application (only for test purpose) all the code is in main function.

I'm using Directx11, DirectMath (xmfloat , xmmatrix , ...) , C++ , VS 2013 ultimate.

I know that without code it's imposible to find the error, but in test project all works fine.unsure.png

Please i need help, i'm stuck

Thanks!!

Advertisement


1) when is debugging (VS 2013) it crash, the typical .exe stop working, but when I run the same program directly with the .exe file it works fine.

Check if your resources folder it is on the same folder of the solution, not at \\Debug.


2) when I'm trying to interact with my classes ( specifically with meshrenderer, material and shader) the program also crash (inside and outside VS)

It is impossible to solve your problem. There is no code. Post the part you think that is wrong.


3) When i'm using only d3d for begin and end scene (not for drawing any gameobject (not with shaders)) everything works fine, but when I include the shader part the program crash. The initialization of the shader result is ok, but the problem is when I add the shader to a material, and that material to a meshrenderer component, and that component to a gameobject (that's why I mean classes interaction)

  1. Activate the DX11 Debug Layer;
  2. Use Intel GPA for graphics debugging;
  3. Use compiled shaders, the errors will be seen before lauching the executable.


4) I have created other project with some classes (not a win32 project,but a console application (without any of directx)) and test the member functions that supposedly fail, and works fine (example : create a gameobject , add a component , get that component , etc).

I would guess a memory corruption, but could be approximately 10^1000 problems according to computer science and the related fields laugh.png .


I have a class that is the core of the engine (named Core) , the game setup occurs in a function named Initialize(). In the console application (only for test purpose) all the code is in main function.

No problem (if you're beginning).


I'm using Directx11, DirectMath (xmfloat , xmmatrix , ...) , C++ , VS 2013 ultimate.

This is the proper IDE. There another math libraries out there such GLM, etc. which I think is more confortable to work with, but this is your option, so... no problem.


I know that without code it's imposible to find the error, but in test project all works fine.

Yes. You've posted on a technical section. Code would help us a lot.


Please i need help, i'm stuck

Don't worry. This is the first of 1000^100 (don't get as a anti-motivation, but it is for sure laugh.png ).

thanks Irlan! smile.png , this are my classes headers in conflict:

MeshRenderer.h


class ColorMaterial;
class MeshRenderer : public Component
{

private:
	ColorMaterial* m_material;

//...

ColorMaterial.h


class MeshRenderer;
class ColorShader;

class ColorMaterial
{
	
private:
	ColorShader* m_ColorShader;
	MeshRenderer* m_MeshRenderer;

//...

ColorShader.h


class ColorMaterial;

class ColorShader
{
private:
	ColorMaterial* m_material;	

The rest of the code (.h and .cpp) i believe is fine. I have been following the tutorials from rastertek.(http://www.rastertek.com/tutdx11.html) rolleyes.gif

I have used forward declarations in these classes to communicate between classes, is this the right way?

thanks Irlan! :) , this are my classes headers in conflict:

MeshRenderer.h

class ColorMaterial;
class MeshRenderer : public Component
{

private:
	ColorMaterial* m_material;

//...
ColorMaterial.h
class MeshRenderer;
class ColorShader;

class ColorMaterial
{
	
private:
	ColorShader* m_ColorShader;
	MeshRenderer* m_MeshRenderer;

//...
ColorShader.h
class ColorMaterial;

class ColorShader
{
private:
	ColorMaterial* m_material;	
The rest of the code (.h and .cpp) i believe is fine. I have been following the tutorials from rastertek.(http://www.rastertek.com/tutdx11.html) :rolleyes:
I have used forward declarations in these classes to communicate between classes, is this the right way?

Yes. Forward declarations reduces dependencies and increases compile time; but there are compile time and run-time, and obviously the problem it is not with compilation because it wouldn't even compile if it was.

Check if you're assigning the addresses to pointers. A NULL pointer can't acess anything.

Try debugging your application using break points and the call stack. On MVS both stays on the bottom right corner of the windows. Once you've figured out which part, post here.

debugging with breakpoints (i put one in the first line of main, then use F11 or F10 to step in or step over)


bool Core::Initialize()
{
	m_hwnd = new HWND;

	int screenWidth = 0, screenHeight = 0;
	bool result;
	
	// Initialize the windows api.
	result = InitializeWindows(screenWidth, screenHeight);   <<--------------------
	

	if (!result)
	{
		MessageBox(*m_hwnd, L"Initialize window FAIL", L"Bienvenido", MB_OK);
		return false;
	}
//...... the rest of initialization

inside InitializeWindows...


bool Core::InitializeWindows(int& screenWidth, int& screenHeight)
{
	WNDCLASSEX wc;
	DEVMODE dmScreenSettings;
	int posX, posY;


	// Get an external pointer to this object.	
	//ApplicationHandle = this;							//MODIFIQUE OJOOOO
	//m_Core = this;
	// Get the instance of this application.
	m_hinstance = GetModuleHandle(NULL);

	// Give the application a name.
	m_applicationName = L"Engine";

	// Setup the windows class with default settings.
	wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = m_hinstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_WINLOGO);
	wc.hIconSm       = wc.hIcon;
	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = m_applicationName;
	wc.cbSize        = sizeof(WNDCLASSEX);
	
	// Register the window class.
	RegisterClassEx(&wc);

	// Determine the resolution of the clients desktop screen.
	screenWidth  = GetSystemMetrics(SM_CXSCREEN);
	screenHeight = GetSystemMetrics(SM_CYSCREEN);

	// Setup the screen settings depending on whether it is running in full screen or in windowed mode.
	if(FULL_SCREEN)
	{
		// If full screen set the screen to maximum size of the users desktop and 32bit.
		memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
		dmScreenSettings.dmSize       = sizeof(dmScreenSettings);
		dmScreenSettings.dmPelsWidth  = (unsigned long)screenWidth;
		dmScreenSettings.dmPelsHeight = (unsigned long)screenHeight;
		dmScreenSettings.dmBitsPerPel = 32;			
		dmScreenSettings.dmFields     = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

		// Change the display settings to full screen.
		ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);

		// Set the position of the window to the top left corner.
		posX = posY = 0;
	}
	else
	{
		// If windowed then set it to 800x600 resolution.
		screenWidth  = 800;
		screenHeight = 600;

		// Place the window in the middle of the screen.
		posX = (GetSystemMetrics(SM_CXSCREEN) - screenWidth)  / 2;
		posY = (GetSystemMetrics(SM_CYSCREEN) - screenHeight) / 2;
	}

	// Create the window with the screen settings and get the handle to it.
	*m_hwnd = CreateWindowEx(WS_EX_APPWINDOW, m_applicationName, m_applicationName,
							WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP,
							posX, posY, screenWidth, screenHeight, NULL, NULL, m_hinstance, NULL);

	
	

	// Bring the window up on the screen and set it as main focus.
	ShowWindow(*m_hwnd, SW_SHOW);
	SetForegroundWindow(*m_hwnd);
	SetFocus(*m_hwnd);
	// Hide the mouse cursor.
	ShowCursor(false);
	
	return true;


}

The crash occurs on "CreateWindowEx", at the moment of step over this function the program stops.

-> Outside VS, with the .exe don't crash

Here is my Core.h


#pragma once


#define WIN32_LEAN_AND_MEAN




const bool FULL_SCREEN = false;
const bool VSYNC_ENABLED = true;
const float SCREEN_DEPTH = 1000.0f;
const float SCREEN_NEAR = 0.1f;


#include "Renderer.h"
#include "inputclass.h"
#include "Camera.h"
#include "ObjectsManager.h"
//
#include "MeshRenderer.h"
#include "ColorMaterial.h"
#include "ColorShader.h"
//
#include "Game.h"


class Core
{
public:
	Core();
	Core(const Core&);
	~Core();

	//bool Initialize(HINSTANCE, HWND, int, int)
	bool Initialize();
	//bool InitializeSingletons
	void Shutdown();
	void Run();

	//get window handler
	
	

	LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM);

	//singletons 
	inline HWND* GetHwnd()
	{
		return m_hwnd;
	}

	inline Renderer* GetRenderer()
	{
		return m_renderer;
	}

	inline ObjectsManager* GetObjManager()
	{
		return m_Objmanager;
	}

	inline InputClass* GetInput()
	{
		return m_Input;
	}

	inline Camera* GetCamera()
	{
		return m_Camera;
	}

	inline Game* GetGame()
	{
		return m_game;
	}

	inline GameObject* GetGameObject()
	{
		return obj1;
	}



private:
	bool Frame();
	bool InitializeWindows(int&, int&);
	void ShutdownWindows();


	LPCWSTR m_applicationName;
	HINSTANCE m_hinstance;
	HWND* m_hwnd;
	
	//Singletons
	Renderer* m_renderer;
	ObjectsManager*	m_Objmanager;
	InputClass* m_Input;
	Game* m_game;
	Camera* m_Camera;

	//test
	GameObject* obj1;
	ColorShader* cube_shader;
	ColorMaterial* cube_material;
	MeshRenderer* cube_meshRenderer;
};



static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


static Core* m_Core = new Core;


The crash occurs on "CreateWindowEx", at the moment of step over this function the program stops.

I can't eyeball the problem.

You need to check the variables that you're passing to the function and see if its value.

This is how I do for my Win32 initialization:


        WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_OWNDC;
	wcex.lpfnWndProc = OnCreateWndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = ::GetModuleHandle(NULL);
	wcex.hIcon = NULL;
	wcex.hCursor = ::LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = _sTitle.c_str();
	wcex.hIconSm = ::LoadIcon(NULL, IDI_APPLICATION);

	if (!::RegisterClassEx(&wcex)) { return false; }

	m_hWnd = ::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, wcex.lpszClassName, wcex.lpszClassName,
		WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
		CW_USEDEFAULT, CW_USEDEFAULT, _ui32Width, _ui32Height,
		NULL, NULL, wcex.hInstance, reinterpret_cast<LPVOID>(this));

	if (!m_hWnd) { return false; }

	::ShowWindow(m_hWnd, _bFullscreen ? SW_SHOWMAXIMIZED : SW_SHOWDEFAULT);

	return true;

The this pointer at the last parameter you don't need to use (though is recommended).

Why are you using a pointer to a HWND? Remove the pointer and just pass an assignment operator.

Previously I had a variable instead of a pointer. Now I have "m_hwnd" variable.
But what is the difference?, I used " *m_hwd ".huh.png

Also now i use "reinterpret_cast<LPVOID>(this)" , because ... you said happy.png

The debugger is still crashing at CreateWindowEx() , with my and with your initialization code . But outside of VS the program seems to be fine, so I wondering if the problem is in the IDE, or the project configuration.

Nothing strange with my variables:

m_hwnd:

- m_hwnd 0x00000000 <NULL> HWND__ *
unused <Unable to read memory>

that's before the CreateWindowEx function.

little question-> can I do all these stuff with a simple main function ( int main() ) ? like in another project with opengl that I made

EDIT: I just download a tutorial from rastertek, the tutorial 4 (draw a green triangle) , and I have used the debugger, it crash at the same function. ohmy.png ( X files music ... )


Previously I had a variable instead of a pointer. Now I have "m_hwnd" variable.

You should stick with this:


HWND hWnd;


Also now i use "reinterpret_cast(this)" , because ... you said

Remove it. This is only needed if you want to use a class as wrapper to your window. Add that to the to do list wink.png !


that's before the CreateWindowEx function.

Nothing wrong. The function returns a valid handle the a window. Try do debug after you call it.


little question-> can I do all these stuff with a simple main function ( int main() ) ? like in another project with opengl that I made

Yes. If your question is about using the console with the window at the same time (int main, not the WinMain), then you can.

OpenGL code should be initialized after you have a working window. You will need a valid HDC to this. Example:


       
	m_hDc = GetDC(hWnd);

	PIXELFORMATDESCRIPTOR m_pfdPixelFormat = {
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,    //Flags
		PFD_TYPE_RGBA,            //The kind of framebuffer. RGBA or palette.
		32,                        //Colordepth of the framebuffer.
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		24,                        //Number of bits for the depthbuffer
		8,                        //Number of bits for the stencilbuffer
		0,                        //Number of Aux buffers in the framebuffer.
		PFD_MAIN_PLANE,
		0,
		0, 0, 0
	};

	int iPixelFormat = ::ChoosePixelFormat(m_hDc, &m_pfdPixelFormat);
	::SetPixelFormat(m_hDc, iPixelFormat, &m_pfdPixelFormat);

	if (!m_hglrcContext) {
		m_hglrcContext = wglCreateContext(m_hDc);
	}
	if (!wglMakeCurrent(m_hDc, m_hglrcContext)) {
		return false;
	}
	if (glewInit() != GLEW_OK) {
		return false;
	}

Wow thanks answer so fast smile.png


Nothing wrong. The function returns a valid handle the a window. Try do debug after you call it.

How do I do that? (Debug after CreateWindowEx)

Wow thanks answer so fast :)

Nothing wrong. The function returns a valid handle the a window. Try do debug after you call it.


How do I do that? (Debug after CreateWindowEx)

Put a breakpoint above the line of the code which you call the function. Click on the line number one time l, you will see a red circle on it. Then click on run. If you have a error, you'll se before you get at the breakpoint.

This topic is closed to new replies.

Advertisement