1

数日前に同様の質問をしましたが、より多くの洞察を求めています。プログラムに文字列を追加すると、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";

その後、例外はなくなります。これは、プログラムの他の場所で発生した不良メモリに (文字列から) 余分なメモリがヒットしただけですか? または、例外は文字列に関連していますか?

助けてくれてありがとう、

ジョー

4

3 に答える 3

3

これらの文字列はグローバル変数であると想定しています。

これらの文字列に別のグローバル変数のコンストラクターから (または main に入る前に呼び出されるメソッドから) アクセスしようとしていますか?

この場合、グローバル変数の初期化順序が複数のコンパイル単位で定義されていないという問題に悩まされています。いくつかの解決策がありますが、アプリケーションに関するより多くの情報が役立つ可能性があります。

メインが入力されているかどうかを確認する最初のテスト。

空の文字列で動作するのは、コンパイラの最適化トリックの結果だと思います。

于 2009-07-07T18:20:54.587 に答える
1

試す

valgrind --leak-check=full your.exe

また、実行可能ファイルのソースコード行を取得するには、-gを使用してアプリケーションをコンパイルすることを忘れないでください。

于 2009-07-07T18:36:58.237 に答える
0

問題は文字列ではありません。プログラムのどこかで、範囲外で読み取りまたは書き込みを行っています。これにより、未定義の動作が発生します。つまり、プログラムが実行されているように見えたり、アクセス違反が発生したり、別のエラーでクラッシュしたり、その他のことが発生する可能性があります。

文字列が違いを生んでいるように見える理由は、プログラムが微妙に変更され、境界外のアクセスが未割り当てのページに到達する原因となるためです。

于 2009-07-07T18:07:01.120 に答える