PC Review


Reply
Thread Tools Rate Thread

delegate for unmanaged callback

 
 
=?Utf-8?B?dGIyMDAw?=
Guest
Posts: n/a
 
      10th Feb 2006
When using a delegate for callback from an unmanaged dll: do I have to fix (
(how do i?) that delegate somehow so that is will not be 'moved' on garbage
collection or voided totaly after the procedure is vacated so the unmanaged
pointer will loose it's relevance?
Thanks!
tb



 
Reply With Quote
 
 
 
 
Daniel Moth
Guest
Posts: n/a
 
      10th Feb 2006
Declare the delegate outside the function at the type level (assuming the
instance of the type is not eligible for GC, neither will the delegate). If
you are still in doubt, post your snippet.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/

"tb2000" <(E-Mail Removed)> wrote in message
news:C84CC5B9-F590-41E2-86FA-(E-Mail Removed)...
> When using a delegate for callback from an unmanaged dll: do I have to fix
> (
> (how do i?) that delegate somehow so that is will not be 'moved' on
> garbage
> collection or voided totaly after the procedure is vacated so the
> unmanaged
> pointer will loose it's relevance?
> Thanks!
> tb
>
>
>



 
Reply With Quote
 
=?Utf-8?B?dGIyMDAw?=
Guest
Posts: n/a
 
      10th Feb 2006
Thanks Daniel, to make sure here's the code snippet:

public class WrapCDLL
{
public delegate void CallBack(IntPtr data);
static WrapCDLL()
....}

public class MyApp
{
public static WrapCDLL.CallBack myNotifyListenerDelegate;
public static void NotifyListener(IntPtr lpUnmanagedData)

{...}

public int Run() //the main loop entered after instantiating
MyApp

{myNotifyListenerDelegate = new
WrapCDLL.CallBack(NotifyListener);
......}
....}



"Daniel Moth" wrote:

> Declare the delegate outside the function at the type level (assuming the
> instance of the type is not eligible for GC, neither will the delegate). If
> you are still in doubt, post your snippet.
>
> Cheers
> Daniel
> --
> http://www.danielmoth.com/Blog/
>
> "tb2000" <(E-Mail Removed)> wrote in message
> news:C84CC5B9-F590-41E2-86FA-(E-Mail Removed)...
> > When using a delegate for callback from an unmanaged dll: do I have to fix
> > (
> > (how do i?) that delegate somehow so that is will not be 'moved' on
> > garbage
> > collection or voided totaly after the procedure is vacated so the
> > unmanaged
> > pointer will loose it's relevance?
> > Thanks!
> > tb
> >
> >
> >

>
>
>

 
Reply With Quote
 
=?Utf-8?B?dGIyMDAw?=
Guest
Posts: n/a
 
      10th Feb 2006
Also - my question probably was unprecise: It's clear that GC won't destroy
the delegate as long as there is a valid reference (!=eligible for GC). But
what about moving it in memory and thereby changing it's address. (I thought
I had read somewhere that the GC might do this with managed types in order to
defragment memory usage?)

"Daniel Moth" wrote:

> Declare the delegate outside the function at the type level (assuming the
> instance of the type is not eligible for GC, neither will the delegate). If
> you are still in doubt, post your snippet.
>
> Cheers
> Daniel
> --
> http://www.danielmoth.com/Blog/
>
> "tb2000" <(E-Mail Removed)> wrote in message
> news:C84CC5B9-F590-41E2-86FA-(E-Mail Removed)...
> > When using a delegate for callback from an unmanaged dll: do I have to fix
> > (
> > (how do i?) that delegate somehow so that is will not be 'moved' on
> > garbage
> > collection or voided totaly after the procedure is vacated so the
> > unmanaged
> > pointer will loose it's relevance?
> > Thanks!
> > tb
> >
> >
> >

>
>
>

 
Reply With Quote
 
Chris Tacke [MVP]
Guest
Posts: n/a
 
      10th Feb 2006
The CF actually keeps an internal pointer to it, so even if it needs to move
itt, it keeps track of that so you don't need to worry about pinning it.

-Chris


"tb2000" <(E-Mail Removed)> wrote in message
news:A3CEED37-1220-47EF-8347-(E-Mail Removed)...
> Also - my question probably was unprecise: It's clear that GC won't
> destroy
> the delegate as long as there is a valid reference (!=eligible for GC).
> But
> what about moving it in memory and thereby changing it's address. (I
> thought
> I had read somewhere that the GC might do this with managed types in order
> to
> defragment memory usage?)
>
> "Daniel Moth" wrote:
>
>> Declare the delegate outside the function at the type level (assuming the
>> instance of the type is not eligible for GC, neither will the delegate).
>> If
>> you are still in doubt, post your snippet.
>>
>> Cheers
>> Daniel
>> --
>> http://www.danielmoth.com/Blog/
>>
>> "tb2000" <(E-Mail Removed)> wrote in message
>> news:C84CC5B9-F590-41E2-86FA-(E-Mail Removed)...
>> > When using a delegate for callback from an unmanaged dll: do I have to
>> > fix
>> > (
>> > (how do i?) that delegate somehow so that is will not be 'moved' on
>> > garbage
>> > collection or voided totaly after the procedure is vacated so the
>> > unmanaged
>> > pointer will loose it's relevance?
>> > Thanks!
>> > tb
>> >
>> >
>> >

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?dGIyMDAw?=
Guest
Posts: n/a
 
      10th Feb 2006

Thanks, wow that was quick!
Cheers
Theo

"Chris Tacke [MVP]" wrote:

> The CF actually keeps an internal pointer to it, so even if it needs to move
> itt, it keeps track of that so you don't need to worry about pinning it.
>
> -Chris
>
>
> "tb2000" <(E-Mail Removed)> wrote in message
> news:A3CEED37-1220-47EF-8347-(E-Mail Removed)...
> > Also - my question probably was unprecise: It's clear that GC won't
> > destroy
> > the delegate as long as there is a valid reference (!=eligible for GC).
> > But
> > what about moving it in memory and thereby changing it's address. (I
> > thought
> > I had read somewhere that the GC might do this with managed types in order
> > to
> > defragment memory usage?)
> >
> > "Daniel Moth" wrote:
> >
> >> Declare the delegate outside the function at the type level (assuming the
> >> instance of the type is not eligible for GC, neither will the delegate).
> >> If
> >> you are still in doubt, post your snippet.
> >>
> >> Cheers
> >> Daniel
> >> --
> >> http://www.danielmoth.com/Blog/
> >>
> >> "tb2000" <(E-Mail Removed)> wrote in message
> >> news:C84CC5B9-F590-41E2-86FA-(E-Mail Removed)...
> >> > When using a delegate for callback from an unmanaged dll: do I have to
> >> > fix
> >> > (
> >> > (how do i?) that delegate somehow so that is will not be 'moved' on
> >> > garbage
> >> > collection or voided totaly after the procedure is vacated so the
> >> > unmanaged
> >> > pointer will loose it's relevance?
> >> > Thanks!
> >> > tb
> >> >
> >> >
> >> >
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Callback from Unmanaged .dll using BSTR strings Mattias Sjögren Microsoft VB .NET 0 10th Jan 2007 06:24 AM
callback function from unmanaged dll using DllImport and delegate Bart Burkhardt Microsoft C# .NET 6 13th Sep 2006 11:12 AM
Help with Threads, and Callback from Unmanaged to Managed Marcus Kwok Microsoft VC .NET 2 25th Apr 2006 03:01 PM
[C++/CLI] Callback from unmanaged to managed Remi THOMAS [MVP] Microsoft VC .NET 3 28th Mar 2006 01:33 PM
Callback example -- Managed C++ and Unmanaged DLL (C++) Kevin Talley Microsoft VC .NET 0 13th Oct 2004 04:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:07 AM.