Jump to content

  • Log In with Google      Sign In   
  • Create Account

Access violation reading location question


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
10 replies to this topic

#1 Conny14156   Members   -  Reputation: 268

Like
0Likes
Like

Posted 31 December 2012 - 05:46 AM

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, 01 January 2013 - 06:25 AM.


Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 13599

Like
2Likes
Like

Posted 31 December 2012 - 06:12 AM

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, 31 December 2012 - 08:01 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 RulerOfNothing   Members   -  Reputation: 1160

Like
0Likes
Like

Posted 31 December 2012 - 06:17 AM

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.

#4 L. Spiro   Crossbones+   -  Reputation: 13599

Like
2Likes
Like

Posted 31 December 2012 - 06:21 AM

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, 31 December 2012 - 07:59 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 Conny14156   Members   -  Reputation: 268

Like
0Likes
Like

Posted 31 December 2012 - 10:01 AM

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, 31 December 2012 - 10:17 AM.


#6 Conny14156   Members   -  Reputation: 268

Like
0Likes
Like

Posted 31 December 2012 - 10:42 AM

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(); 
 

 

 

 

 

 



#7 yckx   Prime Members   -  Reputation: 1210

Like
0Likes
Like

Posted 31 December 2012 - 08:02 PM

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?



#8 Trienco   Crossbones+   -  Reputation: 2173

Like
0Likes
Like

Posted 01 January 2013 - 12:52 AM

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.


f@dzhttp://festini.device-zero.de

#9 Martins Mozeiko   Crossbones+   -  Reputation: 1422

Like
0Likes
Like

Posted 01 January 2013 - 03:09 AM

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.



#10 Conny14156   Members   -  Reputation: 268

Like
0Likes
Like

Posted 01 January 2013 - 03:20 AM

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, 01 January 2013 - 03:33 AM.


#11 Martins Mozeiko   Crossbones+   -  Reputation: 1422

Like
0Likes
Like

Posted 01 January 2013 - 07:30 AM

Put in your code (or inspect with debugger):

printf("%p\n", &Transform->LocalPosition);

Printed address should divide with 16 without reminder.






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