C++ ofstream System.AccessViolation
-
Hello guys, I have a problem. I'm writing to a log file, but when I do the application throws:
An unhandled exception of type 'System.AccessViolationException' occurred in ****
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The code (unmanaged) I'm using is this one:
inline bool writeLog(nsCLog::eSeverity severity, string message)
{
ofstream myfile;if( ((int)severity) >= mLogLevel) { myfile.open(mStrSource.c\_str(), ios::in | ios::out | ios::app); if(!myfile.is\_open()) { throw "Unable to open the file: " + mStrSource; } myfile << "TESTE" << endl; myfile.close(); } } return true;
};
When receiving this error, the code points to the _Lock() of file fstream:
virtual void __CLR_OR_THIS_CALL _Lock()
{ // lock file instead of stream buffer
if (_Myfile)
_CSTD _lock_file(_Myfile);
}Other info: Developed under VS2010 (Incially the project was Winx32 but now it's Winx64) (.NET Framework 4). The open() method of ofstream creates the file and the file is writable but throws the exception when try's to write on the file. Thank you guys in advanced.
-
Hello guys, I have a problem. I'm writing to a log file, but when I do the application throws:
An unhandled exception of type 'System.AccessViolationException' occurred in ****
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The code (unmanaged) I'm using is this one:
inline bool writeLog(nsCLog::eSeverity severity, string message)
{
ofstream myfile;if( ((int)severity) >= mLogLevel) { myfile.open(mStrSource.c\_str(), ios::in | ios::out | ios::app); if(!myfile.is\_open()) { throw "Unable to open the file: " + mStrSource; } myfile << "TESTE" << endl; myfile.close(); } } return true;
};
When receiving this error, the code points to the _Lock() of file fstream:
virtual void __CLR_OR_THIS_CALL _Lock()
{ // lock file instead of stream buffer
if (_Myfile)
_CSTD _lock_file(_Myfile);
}Other info: Developed under VS2010 (Incially the project was Winx32 but now it's Winx64) (.NET Framework 4). The open() method of ofstream creates the file and the file is writable but throws the exception when try's to write on the file. Thank you guys in advanced.
Just for curiosity: why are you throwing a
string
and catching abad_alloc
?THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
Just for curiosity: why are you throwing a
string
and catching abad_alloc
?THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
You are welcome.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
You are welcome.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
Ok guys, For future notice, the problem was related to the configuration of the project. When I converted the project from .net 3.5 to .net 4 and from x32 to x64 something didn't quite go right. So I reverted everything back and converted again in a more controlled environment and now i can open/write/close files without a problem. Cya
-
Hello guys, I have a problem. I'm writing to a log file, but when I do the application throws:
An unhandled exception of type 'System.AccessViolationException' occurred in ****
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The code (unmanaged) I'm using is this one:
inline bool writeLog(nsCLog::eSeverity severity, string message)
{
ofstream myfile;if( ((int)severity) >= mLogLevel) { myfile.open(mStrSource.c\_str(), ios::in | ios::out | ios::app); if(!myfile.is\_open()) { throw "Unable to open the file: " + mStrSource; } myfile << "TESTE" << endl; myfile.close(); } } return true;
};
When receiving this error, the code points to the _Lock() of file fstream:
virtual void __CLR_OR_THIS_CALL _Lock()
{ // lock file instead of stream buffer
if (_Myfile)
_CSTD _lock_file(_Myfile);
}Other info: Developed under VS2010 (Incially the project was Winx32 but now it's Winx64) (.NET Framework 4). The open() method of ofstream creates the file and the file is writable but throws the exception when try's to write on the file. Thank you guys in advanced.
Hello, How you fixed the access violation error on Visual Studio 2010. I am trying to write data to csv file but it is giving me access violation error. As you mentioned, the debugger points to : virtual void __CLR_OR_THIS_CALL _Lock() { // lock file instead of stream buffer if (_Myfile) _CSTD _lock_file(_Myfile); }