我有一個win32 C++應用程序&我得到所有的環境變量&將它們存儲在地圖中。Win32釋放環境變量導致Windows斷點
當我在我的應用程序中調用Win32函數FreeEnvironmentStrings()時,我在MSVC++中出現了一個奇怪的Windows斷點。首先我不知道這是什麼意思&爲什麼它發生?
我該如何解決我的問題&出了什麼問題?
這是我在主函數中調用&導致斷點的一個功能:
std::map <tstring, tstring> GetEnvironmentVariablesEx()
{
// Post: Get all windows environment variables & store in a
// map(key=env.. variable name, value=env variable value)
std::map <tstring, tstring> envVariables;
TCHAR* environVar = GetEnvironmentStrings();
TCHAR* pos = _tcschr(environVar, _T('\0'));
// Skip over the "=::=::\0" of the environVar string
if (pos != NULL) { environVar = ++pos; pos = _tcschr(environVar, _T('\0')); }
else return envVariables;
// I removed the following code because its long & distracting: the error still occurs without the code
// Code: ...use cstring functions to extract environ variables & values & store in map
FreeEnvironmentStrings(environVar); // Breakpoint triggered here: "Windows has triggered a breakpoint in the application. This may be due to a corruption of the heap, which indicates a bug in myApp.exe or any of the DLLs it has loaded."
return envVariables;
}
謝謝:),我有點不確定爲什麼在char數組末尾添加&額外的NULL字符會使它在我釋放時無效? – user593747
說'GetEnvironmentString'例如返回地址'0x1234ABCD'。這個確切的地址是'FreeEnvironmentString'所需要的。在你的代碼中(在'if'塊)你做'environVar = ++ pos;'之後,'environVar'不再是'0x1234ABCD'。所以你不能再給'Free *'函數提供'environVar'。 – Mat