Garbage Collector - Prevent object from being moved

S

SB

What is the best way to prevent an object from being moved by the garbage
collector (for the lifetime of an application)? Pinning seems to be
designed for temporary operations...so I guess that's not an option.

The reason I'm ask is because I have a class that contains many delegates
that are used as callback functions to a C dll. The dll may call any one of
these delegates at any time...so their addresses must be set in stone until
the app is closed.

I believe I can allocate the object on the stack to prevent it from being
affected by the garbage collector..but I'm not sure if that will work or how
to go about doing it.

Any info would be appreciated!

TIA,
sb
 
W

Willy Denoyette [MVP]

You are calling functions (that is JITted code) and this isn't stored in the
GH heap, and as such doesn't 'move'.

Willy.
 
S

SB

Excellent point!

Thanks
sb

Willy Denoyette said:
You are calling functions (that is JITted code) and this isn't stored in
the GH heap, and as such doesn't 'move'.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top