Thanks!! So I am using new
on the C# side when I declare the object as you see below. I’ve
been stepping through the debugger with a watch on myObj, and I noticed that
the “swigCMemOwn member” value happens to have changed to “false”
on the failing iteration.
The “copy of the C#
wrapper” is interesting, but I’m not quite sure how to determine
whether this is what’s happening or not.
One scary thing I noticed while
assigning a watch to the object is that all of the keys in the watch seems to
be recursively nested (if that makes any sense), I’m a bit new to Windows
debugging but this definitely looks odd.
Anyway, if you have any tips on
determing the case you mentioned of “using a copy of the C# wrapper
object instead of the orginal wrapper”, I’d be interested in
understanding that a bit better.
I am suspicious of my
myObj = _o;
So I may take another look at
that as well.
From: David Piepgrass
Sent: Thursday, October 28, 2010 10:44 AM
To: Griffith, John (ISST, Fort Collins CO);
Subject: RE: C# Wrapper PINVOKE System.AccessViolation
These symptoms suggest that the
C++ object has been garbage-collected. If you still have a reference to a
cppObj, this must mean either that you did not allocate it with new on the
C# side (although you say you did), or that you somehow ended up using a
copy of the C# wrapper object instead of the original wrapper. Use a debugger
to make sure that the swigCMemOwn member of cppObj is true.
Another, less likely possibility
is that the cppObj was somehow freed by the C++ code.
From: Griffith, John (ISST, Fort Collins CO)
I hope this is the correct method to ask for help, please
forgive me if not and point me in the correct direction.
My situation is as follows:
I have a C++ DLL and a C# Power Shell API that I’ve
written. We use Swig 2.0.0 to generate a C# wrapper so that I can access
the C++ dll. Everything seems to work great, until I assign my C# object
to a variable in Power Shell. After I run a number of operations using
the power shell variable, I’ll suddenly receive
System.AccessViolation Exception on any PInvoke call that is issued by the SWIG
In my C# code I’m simply doing something like:
Public class MyClass : PSCmdlet
internal cppObj myObj = new
public MyClass(cppObj _o)
myObj = _o;
public string Id
[Cmdlet (VerbsCommon.Get, “MyObj”)]
Public class GetMyObj : myCmdlet
cppvec cpps =
getCppObjs(); //I’ve defined cppvec as a templat in my
wrapper .i file, getCppObjs() just gets all of the C++ objects from the C++ dll
foreach(cppObj o in cpps)
Of course I’ve instantiated myObj via the
constructor. As I’ve mentioned all of this seems to work, until I
assign the C# object to a power-shell variable. I suspect that the
response may be that this appears to be a power shell issue, but I’m
really at a loss as to why I can access said variable a number of times without
issue then suddenly get the exception on PINVOKE.
The Power Shell piece looks something like:
$foo = Get-MyObj –Name someName
Then I can access everything from $foo as expected, however
after a while, I loose the ability to access anything from the C++
dll. When I say “after a while” I mean like if I just
Over and over, after about 5 or 6 times it throws the
exception and pukes.