getting exeception in writting the file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diwakar09
    New Member
    • Sep 2007
    • 39

    getting exeception in writting the file

    Hi,

    I have three classes, each one has distinct role apart from this there is main.
    from main the flows goes as follows
    main-->CTestForLog(cr eate and run the thread)
    and in thread flows goes as.
    CLogger-->CFileWriter(si ngleton class and it writes to a file )

    Means In thread funstion i have instance of CLogger class which calls the write method of CFileWriter class which is a singleton and responsible to write to a single file


    the objective of application is multiple insatnces of CLogger so that all logs goes to a single file.

    Code:
    void CFileWriter::setFileContent(std::string astring)
    	{
    		DWORD nBytes;
    		DWORD nWrites;
    		
    		nBytes = (DWORD)astring.length();
    		
    		WaitForSingleObject(mhmut,INFINITE);
    		
    		LPCVOID temp = astring.c_str();
    		std::cout<<astring<<"\n";
    		WriteFile(mhfile,temp,nBytes,&nWrites,NULL);
    		
    		ReleaseMutex(mhmut);
    	}
    Where
    mhmut = CreateMutex(NUL L,TRUE,NULL);

    but the problem is that i am getting

    Unhandled exception at 0x00421213 in Logger.exe: 0xC0000005: Access violation reading location 0xcccccccc.

    and the application terminate.


    pls. help me
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by diwakar09
    0xC0000005: Access violation reading location 0xcccccccc.
    This is always an uninitialized pointer. Visual Studio loads pointers with 0xcccccccc (always a bad address) when the pointer is created.

    Are you sure mhmut and mhfile are correct?

    Also, std::string::le ngth() is deprecated. You should be using std::string::si ze().
    Also, your function argument should be a reference to avoid an unnecssary copy of the string.
    Also, this is ASCII code and will not work for Unicode. You should be usinmg the TCHAR mappings in tchar.h

    Comment

    • diwakar09
      New Member
      • Sep 2007
      • 39

      #3
      Originally posted by weaknessforcats
      This is always an uninitialized pointer. Visual Studio loads pointers with 0xcccccccc (always a bad address) when the pointer is created.

      Are you sure mhmut and mhfile are correct?

      Also, std::string::le ngth() is deprecated. You should be using std::string::si ze().
      Also, your function argument should be a reference to avoid an unnecssary copy of the string.
      Also, this is ASCII code and will not work for Unicode. You should be usinmg the TCHAR mappings in tchar.h
      Thanks for your reply.
      Now i have changed the whole senario, but couldn't remove that exception,
      May i sent you my files so that you can have a look. if yes give me your mail id,
      any way thanxs once again.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You may post the code here.

        No private conversatuions, please.

        Comment

        Working...