0

I am working on this problem for two days and this is driving me mad as I am still quite new to C++. This violation access problem may be quite easy to you and may be answered thousands of times. But my lack of C++ knowledge make me even unable to identify the same problem ever answered.

OK here is my problem:

1. The major code is in a DLL. I am using Visual Studio 2008

2. This DLL called 3 external libraries: boost, tinyXML and SRILM (a NLP toolkit).

3. The error says: Unhandled exception at 0x5f4f068f (TextNormalizerAPI.dll) in tester.exe: 0xC0000005: Access violation reading location 0x00000000. , occurred only in Debug mode. And the error line was caused by an initialization of a boost::regex object (patUsername = regex("^\\W*@[A-Za-z]");) in my code, but the actual position was deeply inside the boost library, as shown in below figure:

The error occurred in boost lib, not in my code

**in most cases, I am not supposed to change the source code of Boost lib, isn't it? **

4. This error occurred only in Debug version, not in Release version.

5. I replace the whole solution with a old but fault-free version which worked properly in Debug mode. Yet immediately after I generating a Release version of this solution, error occurred in Debug mode!

UPDATE:

6. Thank you guys! I just tried something and found out that even a simple define a regex object in the first line of the entry of DLL will incurred this error! Any ideas?

7. yet initialize a regex object in the first line in the main() of the caller of this dll will not incur this problem.

Hope this description will help you recall something and give me some hint.

I want to ask:

what's the usual strategy to narrow down and spot the problem? thank you!

JXITC
  • 1,110
  • 1
  • 13
  • 27
  • 1
    Reading the call stack (the bottom pain in your screen shot) usually helps a lot. It tells you which function caused the problem. –  Feb 15 '12 at 18:15
  • Visual Studio will do things in debug mode like zero-out memory and maybe pad things that can hide true memory problems. What is the value of m_position at the line with the arrow indicator? – Todd Murray Feb 15 '12 at 18:17
  • @ToddMurray Thank you! Yet the problem is this code is a part of boost library and I assume the problem should not be some coding error, but some linkage or breaking the returning address in stack etc. too complicated for me to identify.. – JXITC Feb 15 '12 at 18:20
  • 1
    @JXITC: We see that the debugger _broke_ in the boost code, but this usually means your code has undefined behavior, namely, an uninitialized or otherwise NULL pointer. In debug, MSVC is smart enough to catch it, but in release, it just fails silently (for speed). – Mooing Duck Feb 15 '12 at 18:29

2 Answers2

3

Looking at your code, you may want to verify that your m_position values are valid ... I'm seeing a escape_type_class_jump label in your code, so the goto or whatever mechanism you're using to jump to that label (I can't tell from the screen-shot) may be bypassing whatever checks are being done to verify that your position increments are still valid.

Jason
  • 31,834
  • 7
  • 59
  • 78
  • Thank you for your kindly answer! Your answer is reasonable but this NULL pointer is occurred in Boost Library code, called from my code while I am creating a regex object `patUsername = regex("^\\W*@[A-Za-z]");` So I don't think I shall modify the boost source code? – JXITC Feb 15 '12 at 18:26
  • BTW, I just added an update to my answer based on what I can see in that screen-shot... – Jason Feb 15 '12 at 18:29
  • MSVC does not initialize variables to 0. It does initialize to some special values such as 0xcdcdcdcd, but not to 0. – Henrik Feb 15 '12 at 18:32
  • thank you @Jason : I just tried something and found out that even a simple define a regex object in the first line of main() will incur this error!? – JXITC Feb 15 '12 at 18:52
  • @JXITC: Are you properly initializing the object? For instance, there has to be a string to parse ... – Jason Feb 15 '12 at 19:11
1

It looks to me like "this" (i.e., the basic_regex_parser) is NULL, and it's trying to call a method on a NULL object, which obviously doesn't work well. If "this" is indeed NULL (you can tell by looking in the "Locals" tab, then I would go up the call stack to the "basic_regex_implementation" level and see what's going on there -- where is this NULL value coming from? In general, looking at different levels of the call stack, and at the variables' values in those levels, will be helpful.

Edward Loper
  • 15,374
  • 7
  • 43
  • 52