views:

50

answers:

2

Hey,

I am required to pass some sort of identifier to unmanaged code which then processes a request and calls back into my managed code once it has done some processing.

I was wondering whether it would be better to create a GCHandle and pass it to the unmanaged code to then recover the object once the unmanaged code passes the GCHandle back or whether it would be better to create a global dictionary of (say integers) which associate the object with that said key.

Thanks for the help!

Till

A: 

If you don't have to actually pass the managed object to unmanaged code, I would vote for a global dictionary. Downside of using dictionary can be need for thread synchronization. Issue with GCHandle is that puts extra burden on GC and you have to do clean-up etc.

VinayC
I was wondering about performance issues as well. I may just run a small test and let you know what gives me better results. Cleaning up shouldn't be too difficult though.
ocelot.fx
A: 

I have just created one million instances of a class and added it to a dictionary by creating random integer keys. Similarly, I created a million objects and created GCHandles for them.

Using GCHandles takes about 60% of the time that it takes to add the objects to the dictionary.

Thanks for the help!

ocelot.fx