Re: [C#] Add code to all non-static methods

William S Fulton <[email protected]> Sun, 31 Oct 2021 22:31:08 +0000
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftCNg4i9e5ChSWw-6bNuSM5POpAiZNbJfgJ6VdMYES-CLw@mail.gmail.com>
Add a 'check' typemap, particularly for SWIGTYPE *self. SWIG uses the
parameter name 'self' when marshalling the this pointer. See
http://swig.org/Doc4.0/Typemaps.html#Typemaps_nn52. You'd need something
like the following for C#:

%typemap(check, canthrow=1) SWIGTYPE *self %{
if (!$1) {
  SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException,
"invalid (null) native object; - Dispose() likely to have already been
called on this object", 0);
  return $null;
}
%}

William


On Wed, 27 Oct 2021 at 21:11, Alexander Vostres <[email protected]> wrote:

> I want to validate "this" pointer on the C# side (swigCPtr). The value of
> null, while valid, produces an AccessViolationException down the stack that
> cannot be caught by default in .NET Framework and at all in .NET Core
> runtimes, which means taking down the entire application which is not
> necessarily a good thing.
>
> On Wed, Oct 27, 2021 at 9:12 PM William S Fulton <[email protected]>
> wrote:
>
>> Are you referring to the 'this' pointer or to parameters in a method? If
>> parameter, that types of parameters? A nullptr is entirely valid of course
>> of pointer types in C++. Maybe you want to modify the default SWIGTYPE *,
>> SWIGTYPE & and SWIGTYPE typemaps.
>>
>> William
>>
>>
>> On Mon, 25 Oct 2021 at 11:08, Alexander Vostres <[email protected]>
>> wrote:
>>
>>> Greetings,
>>> Basically, I'd like to add null check to throw NullReference instead of
>>> getting (non-catchable) AccessViolation from the C++ side.
>>> To do this I need to add something along the lines of "if (ptr == null)
>>> throw;" to all the non-static methods.
>>> So far the closest I've got is modifying csout, however, it applies to
>>> all the methods.
>>> Is there any way to apply a modified csout typemap only to non-static
>>> methods? Or is there any other way to insert couple lines of code into all
>>> the methods?
>>>
>>> Alex
>>> _______________________________________________
>>> Swig-user mailing list
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/swig-user
>>>
>>

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user