Jump to content

  • Log In with Google      Sign In   
  • Create Account


Odd stack write violation in struct (C++)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 lipsryme   Members   -  Reputation: 1006

Like
0Likes
Like

Posted 13 July 2012 - 01:04 PM

Hey guys I hope you can help me out here.
So I've got a header file which looks like this:

/*
****************************************************
GraphicsDescription.h - Header File
Usage: Describes information for rendering
****************************************************
*/

#pragma once
// Library includes
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dx10.lib")
#pragma comment(lib, "DXErr.lib")

// Includes
#include <D3D11.h>
#include <D3DX11.h>
#include <D3DX10.h>
#include <DxErr.h>
#include <xnamath.h>

enum Fill_Mode
{
Solid,
Wireframe,
};

struct GraphicsDescription
{
D3D11_VIEWPORT viewport;
bool VSync;
Fill_Mode fillMode;
};

Now when I run this what I get is the following error:

"An unhandled exception of type 'System.AccessViolationException' occured in Unknown Module."

Now while I was trying around I noticed this odd behavior.

If I replace the enum (or rather the third variable) with anything lower than 2 bytes it works. But anything higher than that .e.g int -> same error.
I've tried placing something like "char padding[256]" in there put that didn't help either.

Anyone knows whats going on here ?

Edited by lipsryme, 13 July 2012 - 01:07 PM.


Sponsor:

#2 fastcall22   Crossbones+   -  Reputation: 4289

Like
0Likes
Like

Posted 13 July 2012 - 01:11 PM

The code compiles and runs fine. (The header file doesn't tell us anything useful if you don't show how you're using it.) Where does it crash?
c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#3 lipsryme   Members   -  Reputation: 1006

Like
0Likes
Like

Posted 13 July 2012 - 01:20 PM

Hmm it seems to crash somewhere inside an xnamathmatrix.inl file in this part here I think:
XMFINLINE _XMMATRIX& _XMMATRIX::operator=
(
	CONST _XMMATRIX& M
)
{
	r[0] = M.r[0];
	r[1] = M.r[1];
	r[2] = M.r[2];
	r[3] = M.r[3];
	return *this;
}


Well if I comment the function that calls this out I still get the same error just a little bit later at another xnamath based function it seems.

Which would be this line:
this->camPosition = XMVectorSet(camDesc.cameraPosition.x, camDesc.cameraPosition.y, camDesc.cameraPosition.z, 0.0f);

Edited by lipsryme, 13 July 2012 - 01:24 PM.


#4 ApochPiQ   Moderators   -  Reputation: 15164

Like
0Likes
Like

Posted 13 July 2012 - 01:22 PM

You're going to need to post a lot more context code for us to have any idea what's going on.

#5 lipsryme   Members   -  Reputation: 1006

Like
0Likes
Like

Posted 13 July 2012 - 01:34 PM

Well I'm not sure where to start. I'm going to give you the code parts that happen between this declaration and the error I guess.

// Define information of our engine
EngineDescription engineDesc;
// Graphics
engineDesc.graphicsDesc.VSync = true;
engineDesc.graphicsDesc.viewport.TopLeftX = 0.0f;
engineDesc.graphicsDesc.viewport.TopLeftY = 0.0f;
engineDesc.graphicsDesc.viewport.MinDepth = 0.0f;
engineDesc.graphicsDesc.viewport.MaxDepth = 1.0f;
engineDesc.graphicsDesc.viewport.Width = (float)engineDesc.windowDesc.windowWidth;
engineDesc.graphicsDesc.viewport.Height = (float)engineDesc.windowDesc.windowHeight;

// Initialize our engine
Engine* engine = new Engine(engineDesc);


Now its being passed to the engine constructor:

Engine::Engine(EngineDescription &engineDesc)
{
// Save data
this->engineDesc = engineDesc;
this->isRunning = false;
// Initialize member
this->renderer = NULL;
this->window = NULL;
this->camera = NULL;

// Initialize Engine
this->Initialize();
}

Next up is the Initialize function:

void Engine::Initialize()
{
// Creates our window
this->window = new Window(this->engineDesc.windowDesc);
// Check if it worked ?
if(!this->window->IsInitialized())
{
  // It failed -> abort engine
  this->isRunning = false;
  MessageBoxW(NULL, L"Failed to initialize Window", L"Engine::Initialize()", MB_OK | MB_ICONERROR);
  return;
}

// Creates our renderer
this->renderer = new Renderer(this->engineDesc.graphicsDesc, this);
// Check if it worked ?
if(!this->renderer->IsInitialized())
{
  // It failed -> abort engine
  this->isRunning = false;
  MessageBoxW(NULL, L"Failed to initialize Renderer", L"Engine::Initialize()", MB_OK | MB_ICONERROR);
  return;
}

This will pass the graphicsDesc to the renderer's constructor:

Renderer::Renderer(GraphicsDescription graphicsDesc, Engine* engine)
{
// Save data
this->graphicsDesc = graphicsDesc;
this->engine = engine;

// Initialize member
this->isInitialized = false;
this->device = NULL;
this->deviceContext = NULL;
this->swapChain = NULL;
this->renderTargetView = NULL;

// Initialize Renderer
this->InitializeRenderer();
}

Then do InitializeRenderer()
void Renderer::InitializeRenderer()
{
HRESULT hr;
// Describe our Buffer
DXGI_MODE_DESC bufferDesc;
ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
bufferDesc.Width = this->engine->GetScreenWidth();
bufferDesc.Height = this->engine->GetScreenHeight();
bufferDesc.RefreshRate.Numerator = (this->graphicsDesc.VSync) ? 60 : 0;
bufferDesc.RefreshRate.Denominator = 1;
bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
// Describe our SwapChain
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
swapChainDesc.BufferDesc = bufferDesc;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = this->engine->GetWindow()->GetHwnd();
swapChainDesc.Windowed = this->engine->IsWindowed();
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
// Create our Swap Chain
hr = D3D11CreateDeviceAndSwapChain(NULL,
		    D3D_DRIVER_TYPE_HARDWARE,
		    NULL, NULL, NULL, NULL,
		    D3D11_SDK_VERSION,
		    &swapChainDesc,
		    &this->swapChain,
		    &this->device,
		    NULL,
		    &this->deviceContext);
if(FAILED(hr))
{
  MessageBoxW(NULL,
	 DXGetErrorDescriptionW(hr),
	 TEXT("D3D11CreateDeviceAndSwapChain"),
	 MB_OK | MB_ICONERROR);
  this->isInitialized = false;
  return;
}

// Create our BackBuffer
ID3D11Texture2D* BackBuffer;
hr = this->swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&BackBuffer);
if(FAILED(hr))
{
  MessageBoxW(NULL,
	 DXGetErrorDescriptionW(hr),
	 TEXT("D3D11CreateDeviceAndSwapChain"),
	 MB_OK | MB_ICONERROR);
  this->isInitialized = false;
  return;
}

// Create our Render Target
hr = this->device->CreateRenderTargetView(BackBuffer, NULL, &this->renderTargetView);
BackBuffer->Release();
if(FAILED(hr))
{
  MessageBoxW(NULL,
	 DXGetErrorDescriptionW(hr),
	 TEXT("D3D11CreateDeviceAndSwapChain"),
	 MB_OK | MB_ICONERROR);
  this->isInitialized = false;
  return;
}

// Describe our DepthStencilBuffer
D3D11_TEXTURE2D_DESC depthStencilDesc;
ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));
depthStencilDesc.Width = this->engine->GetScreenWidth();
depthStencilDesc.Height = this->engine->GetScreenHeight();
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = 1;
depthStencilDesc.SampleDesc.Quality = 0;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
// Create the DepthStencil View
hr = this->device->CreateTexture2D(&depthStencilDesc, NULL, &depthStencilBuffer);
if(FAILED(hr))
{
  MessageBoxW(NULL,
	 DXGetErrorDescriptionW(hr),
	 TEXT("D3D11CreateDeviceAndSwapChain"),
	 MB_OK | MB_ICONERROR);
  this->isInitialized = false;
  return;
}

hr = this->device->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);
if(FAILED(hr))
{
  MessageBoxW(NULL,
	 DXGetErrorDescriptionW(hr),
	 TEXT("D3D11CreateDeviceAndSwapChain"),
	 MB_OK | MB_ICONERROR);
  this->isInitialized = false;
  return;
}
// Set our RenderTarget
this->deviceContext->OMSetRenderTargets(1, &this->renderTargetView, this->depthStencilView);

// Set the Viewport
this->deviceContext->RSSetViewports(1, &this->graphicsDesc.viewport);

// Create the constant buffer
D3D11_BUFFER_DESC cbBufferDesc;
ZeroMemory(&cbPerObjectBuffer, sizeof(D3D11_BUFFER_DESC));
cbBufferDesc.Usage = D3D11_USAGE_DEFAULT;
cbBufferDesc.ByteWidth = sizeof(cbPerObject);
cbBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbBufferDesc.CPUAccessFlags = 0;
cbBufferDesc.MiscFlags = 0;
hr = this->device->CreateBuffer(&cbBufferDesc, NULL, &this->cbPerObjectBuffer);

if(FAILED(hr))
{
  MessageBoxW(NULL,
	 DXGetErrorDescriptionW(hr),
	 TEXT("D3D11CreateDeviceAndSwapChain"),
	 MB_OK | MB_ICONERROR);
  this->isInitialized = false;
  return;
}
// Initialize World Matrix with identity matrix
// this->World = XMMatrixIdentity();
//THIS IS THE LINE WHERE IT CRASHES SOMEWHERE INSIDE ITS XNAMATH file


}


#6 ApochPiQ   Moderators   -  Reputation: 15164

Like
1Likes
Like

Posted 13 July 2012 - 01:39 PM

I suspect the World matrix isn't aligned correctly.

#7 lipsryme   Members   -  Reputation: 1006

Like
0Likes
Like

Posted 13 July 2012 - 01:41 PM

If I comment that line out it crashes at that function I wrote earlier where I set the cameraPosition.

But how is it that I get this odd behavior. I mean if I leave the struct at 2 variables it works perfectly.

Edited by lipsryme, 13 July 2012 - 02:00 PM.


#8 scniton   Members   -  Reputation: 252

Like
2Likes
Like

Posted 13 July 2012 - 03:03 PM

Just taking a shot in the dark:
The following lines look odd to me:
[source lang="cpp"]D3D11_BUFFER_DESC cbBufferDesc;ZeroMemory(&cbPerObjectBuffer, sizeof(D3D11_BUFFER_DESC));[/source]
I think you might have mixed your arguments.

Edited by scniton, 13 July 2012 - 03:06 PM.

Stop twiddling your bits and use them already!

#9 fastcall22   Crossbones+   -  Reputation: 4289

Like
1Likes
Like

Posted 13 July 2012 - 03:17 PM

Just taking a shot in the dark:
The following lines look odd to me:

D3D11_BUFFER_DESC cbBufferDesc;
ZeroMemory(&cbPerObjectBuffer, sizeof(D3D11_BUFFER_DESC));
I think you might have mixed your arguments.


Yes, it is quite possible that cbPerObjectBuffer is smaller in size than D3D11_BUFFER_DESC, causing ZeroMemory to trample over nearby memory. Whether or not this is the main problem remains unknown.

Since this is C++, there is a less error-prone way to zero-initialize structs:
D3D11_BUFFER_DESC cbBufferDesc = {}; // Zero-initialized
// ZeroMemory( &cbBufferDesc, sizeof(cbBufferDesc) ); // Same effect

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#10 DoctorGlow   Members   -  Reputation: 790

Like
0Likes
Like

Posted 13 July 2012 - 03:17 PM

...mistake in response

Edited by DoctorGlow, 13 July 2012 - 03:19 PM.


#11 ApochPiQ   Moderators   -  Reputation: 15164

Like
0Likes
Like

Posted 13 July 2012 - 03:39 PM

Can you post your complete code, e.g. in a zip file? Unfortunately it's really hard to diagnose something like this (since there are probably multiple contributing problems) by just seeing random snippets.

#12 lipsryme   Members   -  Reputation: 1006

Like
0Likes
Like

Posted 14 July 2012 - 05:46 AM

Update: I believe I might have found the problem. According to this page http://www.asawicki...._violation.html
it has something to do with memory alignment and SSE and that you cannot use XMVECTOR or XMMATRIX inside a dynamically allocated structure (like my class).
I'm currently trying to work my way around it using XMFLOAT3 and XMFLOAT4X4 to store them and I think it works. Still need some time to check.

Update2: It works! It really was the problem.

Edited by lipsryme, 14 July 2012 - 06:34 AM.


#13 ApochPiQ   Moderators   -  Reputation: 15164

Like
0Likes
Like

Posted 14 July 2012 - 06:15 PM

For reference, that's what I was referring to here:

I suspect the World matrix isn't aligned correctly.


Although in all fairness it's a big leap from that terse quote to the fix you've got.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS