System.AccessViolationException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SSix2
    New Member
    • Jan 2008
    • 7

    System.AccessViolationException

    .Net 2005 Managed C++

    Background. I am writing a managed c++ wrapper for legacy unmanaged code. I created a test application in C# to create the legacywrapper and I am attempting to get some value returned from legacy.

    I am receiving a System.AccessVi olationExceptio n at the end of this function...

    Code:
    void LegacyWrapper::LegacyWrap::GetValue(String^ dataPath,  Object^% pValue)
    {
        // This stuff is just used to build up the correct legacy database
        // to query information based on a the passed in data path.
        LegacyDatabaseInfo^ dbInfo = gcnew LegacyDatabaseInfo();
        GetDbInfo(dataPath, dbInfo);
        
        // The Application Controller is simply a entrance into the legacy code
        ApplicationController* appCntrl =
            m_legacyCtrl->GetApplicationController(dbInfo->dbID);
        
        string tmp;
        appCntrl->GetValue(dbInfo->fieldID, dbInfo->recordIndex, &tmp);
        String^ pValueStr = gcnew String(tmp.c_str());
    
        if(pValueStr != nullptr)
        {
            pValue = pValueStr;
        }
        else
        {
            pValue = String::Empty;
        }
    
        // **********************************************
        // This is where I get the AccessViolationException
        // **********************************************
    }
    Here is the stack trace...
    LegacyWrapper.d ll!<Module>.CSt dStr<wchar_t>.{ dtor}() + 0x18 bytes
    LegacyWrapper.d ll!<Module>.str ing.{dtor}() + 0x19 bytes
    LegacyWrapper.d ll!LegacyWrappe r::LegacyWrap:: GetValue(System ::String^ dataPath = "DB_REG_MANAGER ID:F_RegDTCSize ID:0", System::Object^ pValue = {System::String ^}) Line 884 + 0x9 bytes C++
    TestLegacy.exe! TestLegacy.Prog ram.Main(string[] args = {Dimensions:[0]}) Line 17 + 0x12 bytes C#


    It looks as if the exception is being thrown when the function is destroying the tmp string...I do not know why...Any suggestions? Thanks in advance...
  • radcaesar
    Recognized Expert Contributor
    • Sep 2006
    • 759

    #2
    An access violation occurs in unmanaged code when it attempts to read or write to memory that has not been allocated, or to which it does not have access. Check whether you had assign values for memory allocation for all the variables you are using.

    Comment

    • SSix2
      New Member
      • Jan 2008
      • 7

      #3
      Originally posted by radcaesar
      An access violation occurs in unmanaged code when it attempts to read or write to memory that has not been allocated, or to which it does not have access. Check whether you had assign values for memory allocation for all the variables you are using.
      Yes memory is being allocated and the values are being modified. Only when the memory is attempting to be destroyed is this exception being thrown...I have no idea why...

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        It's the GC in managed code that's doing the destroying of the string right?

        Comment

        • SSix2
          New Member
          • Jan 2008
          • 7

          #5
          Originally posted by Plater
          It's the GC in managed code that's doing the destroying of the string right?
          I dont know. How can I determine if it is? I think the variable being destroyed is declared on line 12. The exception is thrown on line 28 when it goes out of scope.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I was just wondering if they were like fighting somehow.
            Like the string is created in managed, and populated/linked to data in unmanaged and like the unmanaged code frees up the memory then the GC tries to do it, but the unmanaged has already claimed it as it's own or something.

            I'm really just reaching on this though.

            Comment

            • SSix2
              New Member
              • Jan 2008
              • 7

              #7
              Well what you are saying kind of makes sense. I changed the type to BSTR and used SysAllocString and SysFreeString and things seem to work. This would indicate issues with management of the memory.

              However, now at the end of my main function that is calling the GetValue it throws the same exception but it looks like it is trying to clean up a global string declared in the unmanaged project (i didnt write that crap but I have to deal with it).

              I guess I am looking for the correct project setting that tells the GC to mind its own business...Sinc e I am linking in the unmanaged library does the GC think it is its responsibility?

              Thanks...

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Have you tried marking things "unsafe" and such maybe?

                Comment

                • SSix2
                  New Member
                  • Jan 2008
                  • 7

                  #9
                  Originally posted by Plater
                  Have you tried marking things "unsafe" and such maybe?
                  not really. what would I wrap? all my calls to unmanaged functions?

                  I think there must be something that I am forgetting concerning the global strings...As of right now I am investigating removing these global strings but this is not the optimal solution because it is going to take me forever...

                  I must be doing something wrong in the project file...

                  Comment

                  Working...