[Mono-bugs] [Bug 673645] Method passed to native code as callback is called with 'this' reference equaling 'null'

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Mon Feb 21 03:14:44 EST 2011


https://bugzilla.novell.com/show_bug.cgi?id=673645

https://bugzilla.novell.com/show_bug.cgi?id=673645#c2


Julian Brunner <JulianBrunner at gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |JulianBrunner at gmx.net
         Resolution|                            |INVALID

--- Comment #2 from Julian Brunner <JulianBrunner at gmx.net> 2011-02-21 08:14:43 UTC ---
(In reply to comment #1)
> You should make sure that the delegate which is passed to native code is kept
> alive somehow, i.e. changing your example to:
> 
>     static Action f;
> 
>     static void Main()
>     {
>         f = new Foo ().Bar;
> 
>         initialize(f);
> 
>         while(true) execute();
>     }
> 
> Otherwise, the runtime will collect the delegate, causing all kinds of strange
> crashes, see:
> 
> http://www.mono-project.com/Interop_with_Native_Libraries#Memory_Boundaries
> 
> You can run with MONO_DEBUG=keep-delegates to have the runtime detect this
> error automatically.

Thanks for the quick reply, that did the trick.

I was under the impression that passing a delegate to a native method would
cause it to be retained (and I was stupid enough to never test this), but
seeing how the runtime has no way of knowing when it can be safely collected,
it makes more sense to collect it than to retain it indefinitely.

Sorry for creating a bug report when I should have read the documentation
instead. It just really stumped me that there are situations in which 'this ==
null' evaluates to 'true', but I guess it would be really hard for the runtime
to detect this without considerable performance drawbacks.

Greetings and thanks again,
 Julian

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list