473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

System.AccessViolationException

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
8 4006
radcaesar
759 Expert 512MB
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
SSix2
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
7,872 Expert 4TB
It's the GC in managed code that's doing the destroying of the string right?
Feb 5 '08 #4
SSix2
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
7,872 Expert 4TB
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
SSix2
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
7,872 Expert 4TB
Have you tried marking things "unsafe" and such maybe?
Feb 5 '08 #8
SSix2
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

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

Similar topics

8
by: Richard Lionheart | last post by:
Hi All, I tried using RegEx, but the compiler barfed with "The type of namespace 'RegEx' could not be found. Prior to this, I had the same problem with MatchCollection, but discovered it's...
20
by: djc | last post by:
I get this *intermittently* on a utility I am working on. I don't know whats going on but here are a few points about it: - using VS 2005, running on xp sp2 - program uses multiple threadpool...
0
by: oarrocha | last post by:
I'm trying to use a DLL that was built with Visual C++.NET 7.1 on a new program that I'm developing in VS2005 C# program. Every time I call a function of the 7.1 DLL I receive an anoying...
5
by: deepakkumarb | last post by:
I am getting this exception while executing non query from .NET code to oracle 10g DB. Can you plz help System.AccessViolationException: Attempted to read or write protected memory. This is...
1
by: Creativ | last post by:
A COM component has an method, called Register(Variant variant), which expect an array of string via Variant. If I want to use it, I can pass an object whose element boxes a string. The...
12
by: rmiller407 | last post by:
I am getting an AccessViolationException when calling an old legacy fortran dll from c# in vs2008 express, but the same code worked fine in vs2005. If I create a vs2005 c# wrapper dll to call...
0
by: Dilip | last post by:
I have cross-posted this question to 3 newsgroups since I am not sure exactly where it belongs (and some NG's have very less activity). Apologies in advance for this. I have set the followup to...
2
by: =?Utf-8?B?VHJpc3RhbiBNU0ROIEtlZW4=?= | last post by:
I've written a test harness in C# to test one of our company's products, which is written in C. If the application crashes, the Instruction Address, Memory Address and the Access Type are stored...
5
by: craig1231 | last post by:
I have a problem trying to pass a structure to an unmanaged c++ DLL from C#. When I call PCSBSpecifyPilotLogon from C#, it throws an AccessViolationException This is what I have... The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.