Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ ofstream System.AccessViolation

C++ ofstream System.AccessViolation

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpc++iosdotnet
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kit Fisto
    wrote on last edited by
    #1

    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.

    CPalliniC U 2 Replies Last reply
    0
    • K Kit Fisto

      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.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Just for curiosity: why are you throwing a string and catching a bad_alloc?

      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

      In testa che avete, signor di Ceprano?

      K 1 Reply Last reply
      0
      • CPalliniC CPallini

        Just for curiosity: why are you throwing a string and catching a bad_alloc?

        THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

        K Offline
        K Offline
        Kit Fisto
        wrote on last edited by
        #3

        Hello, It's a typo ;P I'm going to remove it. Thanks :)

        CPalliniC 1 Reply Last reply
        0
        • K Kit Fisto

          Hello, It's a typo ;P I'm going to remove it. Thanks :)

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          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

          In testa che avete, signor di Ceprano?

          K 1 Reply Last reply
          0
          • CPalliniC CPallini

            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

            K Offline
            K Offline
            Kit Fisto
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • K Kit Fisto

              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.

              U Offline
              U Offline
              User 13822763
              wrote on last edited by
              #6

              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); }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups