Using c++ I have a variable that is unexpectedly changing, this could be because I am writing outside allocated memory. The solutions that I have seen are to follow the variable and find that rare place where it should not change, but this is a very slow solution. Is there any way to configure Visual studio debug mode to break when writing outside allocated memory?
Asked
Active
Viewed 123 times
-1
-
Is it possible to implement setter/ getter for this variable? – Eugene Jul 21 '15 at 06:47
-
The question you *should* have asked is "Is there a way to configure Visual studio debug mode to break whenever a given variable changes." - because that is you actual problem. The write outside allocated memory is only a wild guess to what may or may not be the source of the problem. Luckily Ari provided the correct answer. – Arne Mertz Jul 21 '15 at 07:08
-
As I said, that is a solution I already know and it is a slow one, and that's why I am making this question. – porente Jul 21 '15 at 07:17
-
Perhaps if you had provided this information in your original question, you could save us some time. I have updated my answer with the additional info. – Ari0nhh Jul 21 '15 at 07:50
2 Answers
2
You could use a Data breakpoint
to catch the moment when your variable is changing. Data breakpoint could be set using Debug\New breakpoint\New data breakpoint
menu in the Visual Studio.
Edit: To detect memory range violations you could use some profiling tools (e.g Bounds Checker), but this is standalone products you have to buy. Also there is a possibility to write your own memory manager to detect memory range violations. See this, this and this for the details.
-
+1 for answering the question he *should* have asked and providing a solution to the problem instead of answering the question he actually asked :-) – Arne Mertz Jul 21 '15 at 07:09
-
0
Typically you get an Access Violation (0xC0000005) when writing outside Windows allocated memory. But your variable that's accidentally overwritten is definitely in allocated memory.

MSalters
- 173,980
- 10
- 155
- 350