Oh, sorry, I didn’t look closely at your code. getCppObjs() returns a wrapped C++ vector of objects. These objects are owned either by the vector (if they are stored directly in the vector), or by other C++ code (if the vector contains pointers to C++ objects). Assuming the vector contains the C++ objects directly, those objects cease to exist when the vector ceases to exist, and the vector ceases to exist when it is garbage-collected. Also, if you add or remove items in the vector, then all C# wrappers become invalid (in the same way all C++ iterators become invalid).

 

From: Griffith, John (ISST, Fort Collins CO) [mailto:john.griffith2@hp.com]
Sent: Thursday, October 28, 2010 10:55 AM
To: David Piepgrass; swig-user@lists.sourceforge.net
Subject: RE: C# Wrapper PINVOKE System.AccessViolation

 

Hi David,

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 assignment:

myObj = _o;

 

So I may take another look at that as well.

 

Thanks,

John

From: David Piepgrass [mailto:dpiepgrass@mentoreng.com]
Sent: Thursday, October 28, 2010 10:44 AM
To: Griffith, John (ISST, Fort Collins CO); swig-user@lists.sourceforge.net
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) [mailto:john.griffith2@hp.com]

Hello,

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 wrapper.

 

In my C# code I’m simply doing something like:

 

Public class MyClass : PSCmdlet

{

    internal cppObj  myObj = new cppObj();

 

    public MyClass(cppObj _o)

    {

        myObj = _o;

    }

 

   public string Id

   {

       get{return myObj.getID();}

   }

 

  [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)

      {

           WriteObject(new MyClass(o));

      }

  }

}

 

 

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 run:

$foo

 ID

--

Some_id_string

 

$foo

ID

--

Some_id_string

 

 

$foo

ID

--

Some_id_string

 

$foo

                PUKE!!!

 

Over and over, after about 5 or 6 times it throws the exception and pukes.

 

Thanks,

John