4 Replies - 496 Views - Last Post: 15 January 2018 - 01:07 PM Rate Topic: -----

#1 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 762
  • Joined: 04-March 12

DirectX11 - how i inicializate the DirectX11?

Posted 15 January 2018 - 06:09 AM

i'm trying learning how use DirectX 11.
but now i have a memory leak that i don't understand why.
i think that it's a memory leak, because the used memory is increasing and i don't know why.
heres the code:
//Cambalinho.h:
#include <Windows.h>
#include <string>
#include <functional>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#include <DxErr.h>

#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")
#pragma comment (lib, "DxErr.lib")

using namespace std;

class DirectX
{
private:
	HRESULT DirectXResult=0;

public:
	function<void()> Render;
	IDXGISwapChain * DXSwap;
	ID3D11Device *Device;
	ID3D11DeviceContext *DeviceContext;
	DXGI_SWAP_CHAIN_DESC DXSwapdesc;
	ID3D11RenderTargetView *backbuffer;
	ID3D11Texture2D *pBackBuffer;
	D3D11_VIEWPORT ViewPort;


	DirectX(HWND window)
	{
		RECT WindowRect = { 0 };
		GetClientRect(window, &WindowRect);

		//inicializate Directx:
		ZeroMemory(&DXSwapdesc, sizeof(DXGI_SWAP_CHAIN_DESC));
		DXSwapdesc.BufferCount = 1;                                    // one back buffer
		DXSwapdesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;     // use 32-bit color
		DXSwapdesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;      // how swap chain is to be used
		DXSwapdesc.OutputWindow = window;                                // the window to be used
		DXSwapdesc.SampleDesc.Count = 4;                               // how many multisamples
		DXSwapdesc.Windowed = TRUE;                                    // windowed/full-screen mode

																// create a device, device context and swap chain using the information in the scd struct
		D3D11CreateDeviceAndSwapChain(NULL,
			D3D_DRIVER_TYPE_HARDWARE,
			NULL,
			NULL,
			NULL,
			NULL,
			D3D11_SDK_VERSION,
			&DXSwapdesc,
			&DXSwap,
			&Device,
			NULL,
			&DeviceContext);

		//Prepare the View Port:
		DXSwap->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
		DirectXResult=Device->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
		if (backbuffer == NULL|| DirectXResult!=S_OK)
			MessageBox(NULL, "error", "error", MB_OK);
		if(pBackBuffer)
			pBackBuffer->Release();
		DeviceContext->OMSetRenderTargets(1, &backbuffer, NULL);
		
		
		//Create the View Port:
		ZeroMemory(&ViewPort, sizeof(D3D11_VIEWPORT));
		ViewPort.TopLeftX = 0;
		ViewPort.TopLeftY = 0;
		ViewPort.Width = static_cast<float>( WindowRect.right- WindowRect.left);
		ViewPort.Height = static_cast< float>(WindowRect.bottom- WindowRect.top);
		DeviceContext->RSSetViewports(1, &ViewPort);
	}

	void Clear(D3DXCOLOR d3dColor)
	{
		DeviceContext->ClearRenderTargetView(backbuffer, d3dColor);
	}

	void Update()
	{
		DXSwap->Present(0, 0);
	}

	~DirectX()
	{
		//DirectX Clean Up:
		if(DXSwap)
			DXSwap->Release();
		if(backbuffer)
			backbuffer->Release();
		if(Device)
			Device->Release();
		if(DeviceContext)
			DeviceContext->Release();
	}
};

class Form
{
private:
	HWND MainWindow = NULL;
	static HRESULT CALLBACK WndProc(HWND window, UINT message, WPARAM WParam, LPARAM LParam)
	{
		Form *inst =(Form*) GetWindowLongPtr(window, GWLP_USERDATA);
		switch (message)
		{
			case WM_CREATE:
			{
				LPCREATESTRUCT lpCreate = (LPCREATESTRUCT)LParam;
				inst =(Form*) lpCreate->lpCreateParams;
				SetWindowLongPtr(window, GWLP_USERDATA, (LONG_PTR)inst);
			}
			break;
			case WM_CLOSE:
			{
				DestroyWindow(window);
			}
			break;
			case WM_DESTROY:
			{
				PostQuitMessage(0);
			}
			break;
			default:
				return DefWindowProc(window, message, WParam, LParam);
		}
		return 0;
	}

public:
	Form(string Title = "hello World", int PosX = CW_USEDEFAULT, int PosY=CW_USEDEFAULT, int Width=CW_USEDEFAULT, int Height=CW_USEDEFAULT)
	{
		string strWindowClassName = "WindowClassname";
		WNDCLASSEX wndClassName;
		ZeroMemory(&wndClassName, sizeof(WNDCLASSEX));
		wndClassName.cbSize = sizeof(WNDCLASSEX);
		wndClassName.cbClsExtra = 0;
		wndClassName.cbWndExtra = 0;
		wndClassName.hbrBackground = (HBRUSH)(COLOR_WINDOW);
		wndClassName.hCursor = LoadCursor(NULL, IDC_ARROW);
		wndClassName.hIcon = LoadIcon(NULL, IDI_APPLICATION);
		wndClassName.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
		wndClassName.hInstance = GetModuleHandle(NULL);
		wndClassName.lpfnWndProc = (WNDPROC)WndProc;
		wndClassName.lpszClassName = strWindowClassName.c_str();
		wndClassName.lpszMenuName = 0;
		wndClassName.style = CS_HREDRAW | CS_VREDRAW;
		if (!RegisterClassEx(&wndClassName))
			MessageBox(NULL, "Not Registed", "error", MB_OK);
		RECT windrect = { 0,0,Width,Height };
		AdjustWindowRect(&windrect, WS_OVERLAPPEDWINDOW, FALSE);
		MainWindow = CreateWindowEx(WS_EX_CLIENTEDGE, strWindowClassName.c_str(), Title.c_str(), WS_OVERLAPPEDWINDOW, PosX, PosY, windrect.right- windrect.left, windrect.bottom- windrect.top, NULL, NULL, GetModuleHandle(NULL),reinterpret_cast<LPVOID> (this));
		if(MainWindow==NULL)
			MessageBox(NULL, "Not created", "error", MB_OK);
		ShowWindow(MainWindow, SW_SHOWNORMAL);
		//UpdateWindow(MainWindow);
	}

	~Form()
	{
		;
	}
	
	operator HWND()
	{
		return MainWindow;
	}

	static std::function<void()>DirectXRender;

	static WPARAM MessageLoop()
	{
		MSG msgMessage;
		ZeroMemory(&msgMessage, sizeof(MSG));
		while (msgMessage.message != WM_QUIT)
		{
			if (PeekMessage(&msgMessage, NULL, NULL, NULL, PM_REMOVE))
			{
				TranslateMessage(&msgMessage);
				DispatchMessage(&msgMessage);
			}
		}
		return static_cast<int>(msgMessage.wParam);
	}
};
function<void()>Form::DirectXRender = nullptr;

//how use i use it:
#include "cambalinho.h"

Form frmMain("hello World!!!",CW_USEDEFAULT,CW_USEDEFAULT,800,500);
DirectX DirecX(frmMain);

int WINAPI WinMain(HINSTANCE ActualInstance, HINSTANCE NullInstance, LPSTR CommandLine, int WindowShow)
{
	
	DirecX.Render=[]
	{
		DirecX.Clear(D3DXCOLOR(0.0f,0.2f,1.0f,1.0));
		DirecX.Update();
	};
	//Message Loop:
	MSG msgMessage;
	ZeroMemory(&msgMessage, sizeof(MSG));
	while (msgMessage.message != WM_QUIT)
	{
		if (PeekMessage(&msgMessage, NULL, NULL, NULL, PM_REMOVE))
		{
			TranslateMessage(&msgMessage);
			DispatchMessage(&msgMessage);
		}
		if(DirecX.Render)
			DirecX.Render();
	}
	return static_cast<int>(msgMessage.wParam);
}

that's true that i never used DirectX... but on inicialization get a memory leak, it's bad :(
can anyone advice me?

Is This A Good Question/Topic? 0
  • +

Replies To: DirectX11 - how i inicializate the DirectX11?

#2 Radius Nightly   User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 305
  • Joined: 07-May 15

Re: DirectX11 - how i inicializate the DirectX11?

Posted 15 January 2018 - 12:59 PM

It was a bug, dont know if its still a bug, but it has to be fixed to this day.
Are you sure you traced memory leak in the right direction? Peak or reserved memory doesnt mean its all in use.
Was This Post Helpful? 0
  • +
  • -

#3 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 762
  • Joined: 04-March 12

Re: DirectX11 - how i inicializate the DirectX11?

Posted 15 January 2018 - 01:01 PM

i just execute it. then, on Task Manager, i see the memory used increasing :(
i'm using VS2017 and windows 7

This post has been edited by Cambalinho: 15 January 2018 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 Radius Nightly   User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 305
  • Joined: 07-May 15

Re: DirectX11 - how i inicializate the DirectX11?

Posted 15 January 2018 - 01:04 PM

Sometimes you cant depend on Task Manager for a leak. A peak one or over time consume more and more memory?
Was This Post Helpful? 0
  • +
  • -

#5 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 762
  • Joined: 04-March 12

Re: DirectX11 - how i inicializate the DirectX11?

Posted 15 January 2018 - 01:07 PM

yes more and more.. maybe until 15MB's or above
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1