Public Group

# Access violation reading location question

This topic is 2054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Edit:
I Solved it,
Instead of using a struct with XMMATRIX I now use a FLOAT4X4 Instead and just temporary send the value into the XMMATRIX to avoid this 16bit align problem .

Hi,
I was wondering that particular error could mean that my pointer points to a null and am trying to change something that doesn't have something to change right?
but what more could cause that error?

This code doesn't give any compiler error, and I checked with the debugger that "Transform" have "values" which should be changeable.</
<pre class="_prettyXprint _lang-auto _linenums:1">
TransformComponent* temp;
temp = dynamic_cast<TransformComponent*>(pHolder->ComponentList["TransformComponent"].get());
Transform = &temp->Transform;
Transform->LocalPosition = XMMatrixIdentity();
</pre>
Edited by Conny14156

##### Share on other sites

Access violation means you are reading from or writing to memory that has not been flagged as readable/writable, or are executing memory not flagged as executable.

This is not exclusive to NULL (address 0x00000000).

Addresses from 0x00000000 to 0x00010000 are always unreadable on Windows®.

So acting upon any pointer (more specifically, dereferencing any address), NULL or otherwise, which points to invalid addresses will cause access violation.

Executing code requires the address of the code to be flagged as “PAGE_EXECUTE*”—if not then it is an access violation.

One of the trickier cases to detect is stack overflow.  “Transform” may be just one address off the stack, and accessing it then gives access violation.

If you need more help you will need to post the actual line that is giving you this error and preferably a screenshot of what the debugger is showing you for your locals.

L. Spiro

Edited by L. Spiro

##### Share on other sites
If I remember correctly, dynamic_cast returns null if the expression cannot be cast to the intended type. I think you should step through this part of the code to figure out where exactly the access violation is occurring.

##### Share on other sites

Expending on that, the use of dynamic_cast is itself discouraged.  It tends to imply a design problem and always implies a performance problem as it comes with a ton of overhead.

I have personally never used it as there is always a better way.  To be honest, I am not sure I have actually met anyone in person who has used it either and I have never seen it in any production code I have never seen.

L. Spiro

Edited by L. Spiro

##### Share on other sites
Hm,I shall try to do this without using the dynamic cast and see if I can bypass this error >.< thanks for the input
fast edit if dynamic cast is should I try to avoid other casting to? Edited by Conny14156

##### Share on other sites

I followed you guys advice and I tested something else instead for the dynamic cast, and just used a "normal" casting well, its probably not much better but.

I tried to get what I needed from my class the same way I did with my camera class.

It kinda worked, I checked each line, and everything did as it should(from what I could see)

except I get the same error.

        TransformComponent* temp;
Transformers* test;
std::string testS;
temp = (TransformComponent*) pHolder->ComponentList["TransformComponent"]->getMe();
test = temp->getTransform();
testS = temp->getTypeName();
Transform->LocalPosition = XMMatrixIdentity();  <--- Doesnt work

The wierd thing is, the way I did this is exactly the same thing I did with my camera,

I have a function that returns a pointer that points the the desired object

example, and it does indeed work, As I can see the value on my the debug value on "test"

but when I try to change something I get a access error,

BUT

this is nearly identical to how my camera works, but with that I can change value with no problems.

Transformers* TransformComponent::getTransform()
{
return &Transform;
}

The only big diffrent I can think of is that I get the raw pointer from a class object which reside inside a map that reside inside a vector like

Am guessing this is a horrible way to do it as it probably wont allow me to change the inside value?

std::vector<std::shared_ptr<Entity>> vEntity;

void setHolder(Entity* _pHolder);

vEntity[0]->ComponentList["RenderComponent"]->setHolder(vEntity[0].get());
vEntity[0]->ComponentList["RenderComponent"]->Update();

##### Share on other sites

You said you confirmed that temp is valid. Have you stepped through your code and determined that Transform is valid before you try to modify it?

##### Share on other sites

Debug the original code and check the result of your dynamic_cast. If it returns 0, then you absolutely not replace it with a "normal" cast, because it means you probably have a pretty fundamental bug in your class hierarchy. Not using a safe dynamic_cast won't fix anything, it will simply hide the bug and turn it into a minefield of weird crashes (if you're lucky) and very obscure behavior (if you're not so lucky).

dynamic_cast returning 0 clearly tells you "sorry, but that object is not of the type you want". Using a dangerous brute force C cast says "I don't care, just pretend that it is". You then move on to access members that don't exist, execute random code or overwrite memory that belongs to something completely different.

The reason dynamic_cast is often considered "bad" is not the cast itself, but that your requirement to explicitly downcast at all hints at a problem with your design. IF you continue to downcast like that, you should at least keep the dynamic_cast until you decide to abstract things to the point where all your components can use a common interface (hint: directly reading/writing members like you do is not a good start to achieve that).

Basically I see three options:

a) downcast to a specific component and use a unique interface for each type

b) completely pollute the base class with tons of empty virtual functions

c) abstract things to the point of components handling generic messages

If you are only starting out with C++, it might be better not to try and implement a component based system yourself, because finding a path between "doing very ugly things" and "it's a pain to use and causes more problems than it solves" requires a good bit of experience.

##### Share on other sites

Is Transform->LocalPosition member aligned to 16-byte boundary? Afaik in 32-bit builds XMMatrixIdentity() returns XMMATRIX that should be aligned on 16-byte boundary - compiler in your code generates SSE store instruction that assumes it is aligned, and if it won't be aligned you'll get an exception.

##### Share on other sites
Yeha, I kinda expected that this will be hard >.<
But am required to do this for a few reasons.
either way :3
I dont get a error or a null with either of the typecast
It works
In the type I try to cast into have two variables I try to access one of them is a simple string, which I can change without any error whatsoever.
but the other one which is a struct that holds diffrent XMMATRIX doesnt seems to allow me to directly change them without giving me the access violation error. is there something special with just XMMATRIX?

Is Transform->LocalPosition member aligned to 16-byte boundary? Afaik in 32-bit builds XMMatrixIdentity() returns XMMATRIX that should be aligned on 16-byte boundary - compiler in your code generates SSE store instruction that assumes it is aligned, and if it won't be aligned you'll get an exception.

OH I missed yours post while sending my post >.<
Is Transform->LocalPosition member aligned to 16-byte boundary?
not sure what that mean really, gonna google it and see if I can learn something brb for another edit later Edited by Conny14156

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 14
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632152
• Total Posts
3004470

×