字符串太长时的AccessViolationException(C ++)

时间:2009-07-07 18:02:51

标签: c++ string access-violation

几天前我问了一个类似的问题,但我正在寻找更多的见解。我在程序中添加一个字符串时收到AccessViolationException:

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)

该程序在顶层有一堆const std :: string:

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";

如果我用:

替换最后一个字符串
const std::string whiteSpaceBeforeLeadingCmntOption = ""; //"WhiteSpaceBeforeLeadingComment";
然后异常就消失了。这只是额外的内存(来自字符串)击中了我的程序中其他地方引起的一些不良内存吗?或者是与字符串有关的异常?

感谢您的帮助,

3 个答案:

答案 0 :(得分:3)

我认为这些字符串是全局变量。

您是否尝试从另一个全局变量的构造函数(或从输入main之前调用的某个方法)访问这些字符串?

如果是这种情况,您将遇到探测器,即多个编译单元未定义全局变量初始化顺序。有一些解决方案,但有关您的应用程序的更多信息可能会有用。

首先测试是否输入了main。

我认为它使用空字符串是一些编译器优化技巧的结果。

答案 1 :(得分:1)

尝试

valgrind --leak-check=full your.exe

并记得使用-g编译应用程序以获取可执行文件中的源代码行。

答案 2 :(得分:0)

问题不在于字符串。在程序的某个地方,你正在阅读或写出界限。这会导致未定义的行为,这意味着程序可能出现以运行,您可能会遇到访问冲突,或者它可能会因其他错误而崩溃,或者......可能会发生其他任何事情。

字符串出现的原因只是它巧妙地改变了程序,导致越界访问到达未分配的页面。

相关问题