xnamath access violation because of std::string variable

Started by
23 comments, last by yckx 11 years, 3 months ago

Edit:4



I solved it now I think for good.
I removed every xmmatrix and replaced it with XMFLOAT4x4 and I just use xmstore and xmload to transfer the values.




Edit:
Got it to work with the string now after I restarted the compiler but now xnamath give me access violation if I DON'T have the string instead

Edit 2:
Nope, Restared it once again now am back to square one, give me the error if I HAVE the string decalred
am getting more and more confuse for each restart I do sad.png

Edit3
After some more experimenting, xnamath will complain on string depending on whenever it was declared the moment I open the compiler or not.
Example:
If I have the string declared when I start the compiler it will work, but will start to complain whenever I try to remove it/comment out it.
Can someone explain to me whats going on O.O?????





Hi,
For I been trying to figure out why I get xnamath crashes, And after along time and many sacrificed backups I found it.

I have a header file called hA which look like this


#ifndef ModelClass_H#define ModelClass_H#include #include #include #include struct VertexStruct	//Overloaded Vertex Structure{	VertexStruct(){}	VertexStruct(float x, float y, float z,float u, float v,float nx,float ny,float nz ):								Position(x,y,z), UVCordinate(u, v),Normal(nx,ny,nz){}		XMFLOAT3 Position;	XMFLOAT2 UVCordinate;	XMFLOAT3 Normal;};struct FaceStruct{	int vIndex1, vIndex2, vIndex3;	int tIndex1, tIndex2, tIndex3;	int nIndex1, nIndex2, nIndex3;};class ModelHandler{		struct TotalCount		{			int Face;			int Triangle;			int Normal;			int Vertex;			int Test;		};public:	ModelHandler()	{		Reset();	}			void Reset();			TotalCount Total;		std::string bla; <--------------------CRASH if I try to define string here};#endif.h>.h>.h>

and I have another cpp file called cppA and header file called hB that is TOTAL UNRELATED (for now) and either cppA or hB is using ANYTHING from hA.

cppA file look like this


#include #include #include "ObjectH.h"#include "RenderComponentH.h"#include "DirectXHandlerH.h"#include "CameraHandlerH.h"#include bool RenderComponent::Initializing(){	ID3D11Device* pD3d11Device;	pD3d11Device = Get3DDevice();		Vertex* pVertices;		pVertices = new Vertex[5];	Vertex v[] =	{			// Front Face		Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f),		Vertex(-1.0f,  1.0f, -1.0f, 0.0f, 0.0f),		Vertex( 1.0f,  1.0f, -1.0f, 1.0f, 0.0f),		Vertex( 1.0f, -1.0f, -1.0f, 1.0f, 1.0f),		// Back Face		Vertex(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f),		Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 1.0f),		Vertex( 1.0f,  1.0f, 1.0f, 0.0f, 0.0f),		Vertex(-1.0f,  1.0f, 1.0f, 1.0f, 0.0f),		// Top Face		Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f),		Vertex(-1.0f, 1.0f,  1.0f, 0.0f, 0.0f),		Vertex( 1.0f, 1.0f,  1.0f, 1.0f, 0.0f),		Vertex( 1.0f, 1.0f, -1.0f, 1.0f, 1.0f),		// Bottom Face		Vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f),		Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 1.0f),		Vertex( 1.0f, -1.0f,  1.0f, 0.0f, 0.0f),		Vertex(-1.0f, -1.0f,  1.0f, 1.0f, 0.0f),		// Left Face		Vertex(-1.0f, -1.0f,  1.0f, 0.0f, 1.0f),		Vertex(-1.0f,  1.0f,  1.0f, 0.0f, 0.0f),		Vertex(-1.0f,  1.0f, -1.0f, 1.0f, 0.0f),		Vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f),		// Right Face		Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 1.0f),		Vertex( 1.0f,  1.0f, -1.0f, 0.0f, 0.0f),		Vertex( 1.0f,  1.0f,  1.0f, 1.0f, 0.0f),		Vertex( 1.0f, -1.0f,  1.0f, 1.0f, 1.0f),	};	DWORD indices[] = 	{		// Front Face		0,  1,  2,		0,  2,  3,		// Back Face		4,  5,  6,		4,  6,  7,		// Top Face		8,  9, 10,		8, 10, 11,		// Bottom Face		12, 13, 14,		12, 14, 15,		// Left Face		16, 17, 18,		16, 18, 19,		// Right Face		20, 21, 22,		20, 22, 23	};	//Create the vertex buffer		D3D11_BUFFER_DESC indexBufferDesc;	ZeroMemory( &indexBufferDesc, sizeof(indexBufferDesc) );	indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;	indexBufferDesc.ByteWidth = sizeof(DWORD) * 12 * 3;	indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;	indexBufferDesc.CPUAccessFlags = 0;	indexBufferDesc.MiscFlags = 0;	D3D11_SUBRESOURCE_DATA iinitData;	iinitData.pSysMem = indices;	pD3d11Device->CreateBuffer(&indexBufferDesc, &iinitData, &Buffer.Model.pIndexBuffer);	//Create the vertex buffer Describer and describ it	D3D11_BUFFER_DESC vertexBufferDesc;	ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );	vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;	vertexBufferDesc.ByteWidth = sizeof( Vertex ) * 24;	//vertexBufferDesc.ByteWidth = sizeof( Vertex ) * Model.VertexCount;	vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;	vertexBufferDesc.CPUAccessFlags = 0;	vertexBufferDesc.MiscFlags = 0;	D3D11_SUBRESOURCE_DATA vertexBufferData; 	ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );	vertexBufferData.pSysMem = v;	pD3d11Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &Buffer.Model.pVertexBuffer);		delete [] pVertices;	pVertices = 0;		HRESULT hr;	D3D11_BUFFER_DESC cbbd;		ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));	cbbd.Usage = D3D11_USAGE_DEFAULT;	cbbd.ByteWidth = sizeof(ConstantRenderBuffer);	cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;	cbbd.CPUAccessFlags = 0;	cbbd.MiscFlags = 0;	hr = pD3d11Device->CreateBuffer(&cbbd, NULL, &Buffer.Model.pcObjectBuffer);	RenderState = GetWireFrameState();	return true;}bool RenderComponent::Special(){	ID3D11DeviceContext* pD3d11DevCon;	pD3d11DevCon = Get3DDevCon();	UINT stride = sizeof( Vertex );	UINT offset = 0;	//Set the vertex 	pD3d11DevCon->IASetIndexBuffer(Buffer.Model.pIndexBuffer, DXGI_FORMAT_R32_UINT, 0);	pD3d11DevCon->IASetVertexBuffers( 0, 1, &Buffer.Model.pVertexBuffer, &stride, &offset );			//Set Correct Model Buffer	pD3d11DevCon->UpdateSubresource(Buffer.Model.pcObjectBuffer, 0, NULL, &Buffer.cBufferObject, 0, 0 );	pD3d11DevCon->VSSetConstantBuffers( 0, 1, &Buffer.Model.pcObjectBuffer);		//Set Texture	pD3d11DevCon->PSSetShaderResources( 0, 1, &Buffer.Texture.Diffuse.Texture );	pD3d11DevCon->PSSetSamplers( 0, 1, &Buffer.Texture.Diffuse.Sampler );	pD3d11DevCon->RSSetState(NULL);	pD3d11DevCon->DrawIndexed(36,0,0);			//Set Vertex and Pixel Shaders	//pD3d11DevCon->VSSetShader(Shader.VS, 0, 0);    //pD3d11DevCon->PSSetShader(Shader.PS, 0, 0);	return true;	}void RenderComponent::Update(){	TransformComponent tTransformComponent;	TransformMatrix tMatrixTransform;	pHolder->ComponentList["TransformComponent"]->getMe(tTransformComponent);		tMatrixTransform.LocalPosition = XMLoadFloat4x4(&tTransformComponent.Transform.LocalPosition);	tMatrixTransform.Rotation = XMLoadFloat4x4(&tTransformComponent.Transform.Rotation);	tMatrixTransform.Scale = XMLoadFloat4x4(&tTransformComponent.Transform.Scale);	tMatrixTransform.WorldPosition= XMLoadFloat4x4(&tTransformComponent.Transform.WorldPosition);		if(true)	{		Camera*	pTheCamera;		pTheCamera = GetCamera();				//Reset The Position(Still not sure if its Local or world or whatever)		//tMatrixTransform.LocalPosition = XMMatrixIdentity();		//tMatrixTransform.Translation = XMMatrixTranslation( 0.0f, 0.0f, 4.0f );		//Set cube1's world space using the transformations		tMatrixTransform.WorldPosition = tMatrixTransform.LocalPosition * tMatrixTransform.Scale * tMatrixTransform.Rotation;		tMatrixTransform.WorldPosition = tMatrixTransform.LocalPosition * pTheCamera->View * pTheCamera->Projection;Buffer.cBufferObject.WVP = XMMatrixTranspose(tMatrixTransform.WorldPosition); <---------THIS ONE is the one that CRASH	}	else	{		MessageBox(NULL,"The Entity Object has no Transform Component you sure this is correct?","Error",MB_OK);		}}void RenderComponent::Destroy(){	Buffer.Model.pIndexBuffer->Release();	Buffer.Model.pVertexBuffer->Release();	Buffer.Model.pcObjectBuffer->Release();	Buffer.Texture.Diffuse.Sampler->Release();	Buffer.Texture.Diffuse.Texture->Release();	//RenderState->Release();	//Buffer.Lightning.pVSBuffer->Release();	//Buffer.Lightning.pPSBuffer->Release();}void RenderComponent::setHolder(Entity* _pHolder){	pHolder = _pHolder;			MessageBox(0,"It Works","Yay",MB_OK);	}void RenderComponent::getMe(Buffers& pRender){	pRender = Buffer;}bool RenderComponent::LoadTexture(){	HRESULT hr;	ID3D11Device* pD3d11Device;	pD3d11Device = Get3DDevice();	hr = D3DX11CreateShaderResourceViewFromFile(pD3d11Device ,Buffer.Texture.Diffuse.Location.c_str(),NULL, NULL, &Buffer.Texture.Diffuse.Texture, NULL );	if(FAILED(hr))	{		MessageBox(0,DXGetErrorDescription(hr),"Error",MB_OK);	}	D3D11_SAMPLER_DESC sampDesc;	ZeroMemory( &sampDesc, sizeof(sampDesc) );	sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;	sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;	sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;    sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;    sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;    sampDesc.MinLOD = 0;    sampDesc.MaxLOD = D3D11_FLOAT32_MAX;	pD3d11Device->CreateSamplerState(&sampDesc,&Buffer.Texture.Diffuse.Sampler);	return true;}std::string RenderComponent::getTypeName(){	return typeName;}.h>.h>.h>

while hB look like


#ifndef RenderComponent_H#define RenderComponent_H#include #include #include #include #include "BaseComponentH.h"#include "EntityHandlerH.h"#include "TransformComponentH.h"struct ConstantRenderBuffer{	XMMATRIX WVP;};struct Buffers{	struct ModelBuffer	{		ID3D11Buffer* pVertexBuffer;		ID3D11Buffer* pIndexBuffer;		ID3D11Buffer* pcObjectBuffer;	};	struct LightningBuffer	{		ID3D10Blob* pVSBuffer;		ID3D10Blob* pPSBuffer;	};	struct TextureBuffer	{		struct DiffuseObjects		{			ID3D11ShaderResourceView* Texture;			ID3D11SamplerState* Sampler;			std::string Location;		};		DiffuseObjects Diffuse;	};	ModelBuffer Model;	TextureBuffer Texture;	LightningBuffer Lightning;	ConstantRenderBuffer cBufferObject;};class RenderComponent: public BaseComponent{private:	public:	RenderComponent()	{		typeName = "RenderComponent";		Buffer.Texture.Diffuse.Location = "Test.jpg";		LoadTexture();		Initializing();	}	RenderComponent(Entity* pComponentHolder)	{		typeName = "RenderComponent";		pHolder = pComponentHolder;	}	~RenderComponent(){}	bool Initializing();	bool Special();	void Update();	void Destroy();	void setHolder(Entity* _pHolder);	void getMe(Buffers& pRender);	bool LoadTexture();	std::string getTypeName();	std::string typeName;	ID3D11RasterizerState* RenderState;	Buffers Buffer;	Entity* pHolder;};#endif.h>.h>.h>

and I have a main.cpp
that which call upon the RenderComponent.Update()
If I dont declare std::string inside hA header, Everything work as it should be.
but If I try to declare std::string inside the header


XMMatrixTranspose do not work after I declare and just crash upon reaching that line.

but the program will work if I just comment/remove the line. but why would it crash in the first place?

Advertisement

Don't really know if i can help, but try changing the order of the includes, for example, try including <string> before anything else, or after anything else... Seriously don't really know what the issue could be...

Nope doesn't work :(, still give me acess violation error :/.

P.S

hB have nothing to do with hA. so I don't think it could be anything with the include order :(

I got it partly working now by restarting the compiler.
But am still very confuse why xnamath gave me access violation (and still give) if I try to change the string o.o? if I don't rebuild it, even tho neither headers /cpp have anything to do with each other.
I should had tried to rebuild it before I posted here >.< sorry for wasting the time


Edited
PS
Now xnamath give's me the error if I don't have the string declared o.o

Edit2:
PS2

Back to square one after restarting again, give me the error when I HAVE the string declared

It's difficult to go through your code on my phone, but it sounds like things that can happen if an XMMATRIX isn't aligned in memory. Is the variable you're writing to allocated on the heap? Or owned by an object that is allocated on the heap?

Edit: taking another look at it, I'm guessing your RenderComponent object is heap allocated. XMMATRIX needs to be 16 bit aligned in memory. If it's on the stack then it's pretty much done for you, due to how XMMATRIX is defined. But on the heap, you have to make sure it's properly aligned yourself. When I have a chance to get on my pc I can post some links that can explain how to deal with this, but you can be proactive and google "heap aligned memory" or something similar. I remember there's a gamasutra article that explains it quite well.

Am sorry, iam still not that advance with c++ yet, so am not 100% sure what heap mean :x,

but if you mean about the xmmatrix if its allign to 16byte or not. it should be, as load the value from a xmfloat into a xmmatrix. to avoid that problem (I had it once before >.<)

oh but the xmmatrix is inside a struct, but I declare it inside the function.

Try having the string declared but comment the XNMatrixTranspose line and see if it crashes.

If it doesn't, instead of performing the XNMatrixTranspose operation like this


Buffer.cBufferObject.WVP = XMMatrixTranspose(tMatrixTransform.WorldPosition);

try it like this


XMMatrixTranspose(tMatrixTransform.WorldPosition);

it's just to see if it crashes.

Again, i'm as lost as you on this, but keep trying and say something.

Already tested with commenting out the Transpose when I had the string, it does indeed not crash

and just tested out the XMMatrixTranspose without assigning it to the buffer, it does indeed not crash.

So am guessing it has something to do with my buffer o.o? but why? and HOW O.O?

The heap is the part of memory where dynamically declared objects live. So, anything created with "new". It looks like the XMMATRIX you're writing to is a member of a strict that's a member of a RenderComponent object. If you created that object with "new" then that's what's throwing you off.

not creating it with new, and it doesn't give me any of the align 16 byte error anymore (for now) atleast.

This topic is closed to new replies.

Advertisement