Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Is GCHandle.Alloc thread safe?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JA - 29 Jun 2007 22:22 GMT
I use a method for threading that instantiates an object that is a wrapper to
a DLL (written in C).

The wrapper class is passed a byte array, and then does

GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
IntPtr ptr = (IntPtr)(handle.AddrOfPinnedObject().ToInt32() +
buffer.Length-1);

It passes ptr to the DLL function FunctionX().

I've run my application in debug mode, and it errors calling FunctionX()
with more than one thread. A single thread does not cause a problem.

When I check Autos for each of the threads, one thread shows "handle" above
as being correctly allocated, and handle.Target = {System.Array}. The other
thread shows "error: cannot obtain value" for

handle.__InternalTarget
handle.IsAllocated
handle.Target

So is "handle" above thread safe? Is there a better way to do this? Thanks,

JA
JA - 29 Jun 2007 23:38 GMT
By the way, "buffer" below is the byte array I'm passing to FunctionX().

> I use a method for threading that instantiates an object that is a wrapper to
> a DLL (written in C).
[quoted text clipped - 21 lines]
>
> JA

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.