Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have declared a vector as a member variable in a class in my program. I am not even using it but still it gives access violation error at runtime.

Why is it so ??

vector <int> m_iNumbers;
Posted
Updated 5-Jul-11 23:08pm
v2
Comments
ThatsAlok 6-Jul-11 5:22am    
i doubt it...could you let us know what the actual error!
PrafullaVedante 6-Jul-11 6:06am    
Error comes at runtime .... Program shows error about memory corruption at the exit.
Sergey Alexandrovich Kryukov 6-Jul-11 11:14am    
There is nothing wrong in the code line you show. The problem is elsewhere. You do not provide sufficient information.
--SA

The other cause could be that a memory overwrite exists in the previously existing code. It now overwrites the new member variable and thus corrupts it.

You can check for this by adding a buffer before the new variable, init to 0 in the constructor and check the content at various points to see if/when its being corrupted.
 
Share this answer
 
Comments
CPallini 6-Jul-11 9:02am    
Good point, indeed.
Niklas L 6-Jul-11 15:22pm    
You can even set a break point in VS to trigger when that variable changes, to quickly find the offending module. 5.
I think just inclusion wont give any error.If you are using Microsoft visual studio, Just start debugger and when the error came click on retry button which takes you to the error code and check from where the error is coming.
 
Share this answer
 
I just added that line to my program and it has no effect, you must be doing something else to cause the crash.
 
Share this answer
 
Comments
Niklas L 6-Jul-11 7:16am    
Did you actually add it to your own code to verify that? Something tells me you're not being completely honest here :)
Richard MacCutchan 6-Jul-11 7:29am    
Yes, I did add it to my own code. I do not see why you need to imply that I'm being dishonest!
Niklas L 6-Jul-11 9:39am    
Simply because your're an excellent C++ programmer. I imply that you knew before testing it that the inclusion of the member variable wasn't the real problem, and that you also knew your own code would work with or without it. If you could not add a vector<int> without having your code break, not much code would work.
It was meant as a compliment rather than an accusation, even if it was somewhat disguised.
Richard MacCutchan 6-Jul-11 10:49am    
My mistake; I guess I misinterpreted your message, and also did not quite notice the smiley.
Oh, BTW, I am far from being an excellent C++ programmer; but I am willing to learn.
The line itself does almost nothing but making your class a little wider.
If you are working with multiple files, check the inclusion dependencies (it seems you class is destroyed in a file whose obj file doesn't know about the change you made: probably a full rebuild will eliminated the problem).

Or... you used the vector using the [] operator (that doesn't check form bounds ...) outside a proper range (causing corruption in a memory guard, that is revealed during the system cleanup)

Or ... many other (bad) things
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900