If by using a local variable you mean local field, then yes this is reliable.

From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On 
Behalf Of Greg Keogh
Sent: Sunday, October 31, 2010 1:44 PM
To: 'ozDotNet'
Subject: FW: Use of GCHandle.Alloc and Free

You may recall two weeks ago I had to use code like this to stop a COM object 
from being collected:

GCHandle cursorHandle = GCHandle.Alloc(feature.Cursor, GCHandleType.Normal);

A few people suggested that it was overkill and simply making a local variable 
reference to feature.Cursor would fix the problem.

It does!

Although I wonder if using a dummy reference is reliable. In Debug mode it 
works, but in Release mode might it get optimised away and my problem will 
return? I didn't try it.

Greg

Reply via email to