By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

System.AccessViolationException

P: 7
.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.AccessViolationException at the end of this function...

Expand|Select|Wrap|Line Numbers
  1. void LegacyWrapper::LegacyWrap::GetValue(String^ dataPath,  Object^% pValue)
  2. {
  3.     // This stuff is just used to build up the correct legacy database
  4.     // to query information based on a the passed in data path.
  5.     LegacyDatabaseInfo^ dbInfo = gcnew LegacyDatabaseInfo();
  6.     GetDbInfo(dataPath, dbInfo);
  7.  
  8.     // The Application Controller is simply a entrance into the legacy code
  9.     ApplicationController* appCntrl =
  10.         m_legacyCtrl->GetApplicationController(dbInfo->dbID);
  11.  
  12.     string tmp;
  13.     appCntrl->GetValue(dbInfo->fieldID, dbInfo->recordIndex, &tmp);
  14.     String^ pValueStr = gcnew String(tmp.c_str());
  15.  
  16.     if(pValueStr != nullptr)
  17.     {
  18.         pValue = pValueStr;
  19.     }
  20.     else
  21.     {
  22.         pValue = String::Empty;
  23.     }
  24.  
  25.     // **********************************************
  26.     // This is where I get the AccessViolationException
  27.     // **********************************************
  28. }
  29.  
Here is the stack trace...
LegacyWrapper.dll!<Module>.CStdStr<wchar_t>.{dtor} () + 0x18 bytes
LegacyWrapper.dll!<Module>.string.{dtor}() + 0x19 bytes
LegacyWrapper.dll!LegacyWrapper::LegacyWrap::GetVa lue(System::String^ dataPath = "DB_REG_MANAGERID:F_RegDTCSizeID:0", System::Object^ pValue = {System::String^}) Line 884 + 0x9 bytes C++
TestLegacy.exe!TestLegacy.Program.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...
Feb 4 '08 #1
Share this Question
Share on Google+
8 Replies


radcaesar
Expert 100+
P: 759
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.
Feb 5 '08 #2

P: 7
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...
Feb 5 '08 #3

Plater
Expert 5K+
P: 7,872
It's the GC in managed code that's doing the destroying of the string right?
Feb 5 '08 #4

P: 7
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.
Feb 5 '08 #5

Plater
Expert 5K+
P: 7,872
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.
Feb 5 '08 #6

P: 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...Since I am linking in the unmanaged library does the GC think it is its responsibility?

Thanks...
Feb 5 '08 #7

Plater
Expert 5K+
P: 7,872
Have you tried marking things "unsafe" and such maybe?
Feb 5 '08 #8

P: 7
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...
Feb 5 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.