views:

274

answers:

3

Hi all,

I asked a similar question a couple of days ago, but I'm looking for more insight. I'm getting an AccessViolationException when I add a string to my program:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at _cexit()
   at <CrtImplementationDetails>.LanguageSupport._UninitializeDefaultDomain(Void * cookie)
   at <CrtImplementationDetails>.LanguageSupport.UninitializeDefaultDomain()
   at <CrtImplementationDetails>.LanguageSupport.DomainUnload(Object source, EventArgs arguments)
   at <CrtImplementationDetails>.ModuleUninitializer.SingletonDomainUnload(Object source, EventArgs arguments)

The program has a bunch of const std::string's at the top level:

const std::string caseDelimitedOption = "CaseDelimited";
const std::string endOfLineOption = "EndOfLine";
const std::string functionNameDelimitWordsOption = "FunctionNameDelimitWords";
const std::string functionNameStartCaseOption = "FunctionNameStartCase";
const std::string indentStringOption = "IndentString";
const std::string lowerCaseOption = "LowerCase";
const std::string newLineBetweenDoAndWhileOption = "NewLineBetweenDoAndWhile";
const std::string nextLineOption = "NextLine";
const std::string nextLineAsWellAsCloseParenOption = "NextLineAsWellAsCloseParen";
const std::string noBracesAroundSingleStatementBlockOption = "NoBracesAroundSingleStatementBlock";
const std::string openBraceLocationOption = "OpenBraceLocation";
const std::string underscoreDelimitedOption = "UnderscoreDelimited";
const std::string upperCaseOption = "UpperCase";
const std::string whiteSpaceBeforeLeadingCmntOption = "WhiteSpaceBeforeLeadingComment";

If I replace last string with:

const std::string whiteSpaceBeforeLeadingCmntOption = ""; //"WhiteSpaceBeforeLeadingComment";

then the exception goes away. Is this just extra memory (from the string) hitting some bad memory that was caused elsewhere in my program? Or is the exception related to the string?

Thanks for any help,

Joe

A: 

The problem isn't the string. Somewhere in your program, you're reading or writing out of bounds. This results in undefined behavior, meaning that the program might appear to run, you might get an access violation, or it might crash with another error, or... anything else might happen.

The reason the string appears to make a difference is simply that it changes the program subtly, causing the out-of-bounds access to reach an unallocated page.

jalf
Gotcha. I was hoping that wasn't the case.
Joe
+2  A: 

I assume these strings are global variables.

Are you trying to access these strings from the constructor of another global variable (or from some method that is called before main is entered)?

If this is the case you are suffering from the probelem that global variable initialization order is undefined across multiple compilation units. There are a few solutions but more information about your application would probably be useful.

First test to see if main is even entered.

I would think that it working with the empty string is the result of some compiler optimization trick.

Martin York
Main is entered, but thanks for the idea. You might be right about the optmization trick because when I turn all the optimizations off, the exception goes away. Hmmm ....
Joe
Try defining all of those constants as const char * instead of const strings, and see what happens.
Joe
Interesting ... when I did this, I got a bunch of linker errors saying the char * was already defined.
Joe
You need to declare them in a header file, then define them in a single .cpp file. Otherwise you get linker errors.
Martin York
Thanks. Appreciate it.
Joe
+1  A: 

Try

valgrind --leak-check=full your.exe

and remember to compile your application with -g to get source code line in the executable.

xcramps
Thanks. I'll look into this.
Joe