« Selected posts on CLR, threading, Internationalization and WPF | Main | My quest to select a good Antivirus for the year 2008 »

May 21, 2008


TrackBack URL for this entry:

Listed below are links to weblogs that reference Memory control : Use GCHandle to pin down the objects:


Leon Anderson

Hey there, great topic! I am trying to create a poor mans singleton class where I can use the New() to pass reference to the shared object. I am trying to do this with this bit of code, however it isn't quite working how I had hoped...maybe you can guide me, seeing as you are quite advanced with this topic.

Any help would be greatly appreciated!
Private Shared oSession As Session = Nothing
Public Sub New(ByVal viSessionId As Integer)
If oSession Is Nothing OrElse (oSession.SessionId <> viSessionId) Then
SessionId = viSessionId
miCieId = SYSessio.cie_id
miSuccId = SYSessio.succ_id
miDeptId = SYSessio.dept_id
miUserId = SYSessio.cle_user
If oSession Is Nothing Then
oSession = Me
End If
Dim gh As System.Runtime.InteropServices.GCHandle
gh = System.Runtime.InteropServices.GCHandle.Alloc(Me)
gh.Target = oSession -> problem here!
'oSession is ok, but gh.Target not pointing to oSession after here.
End If
End Sub

Public Shared Function GetCurrentSessionObject() As Session
If oSession Is Nothing Then
oSession = New Session(CInt(System.Threading.Thread.CurrentPrincipal.Identity.Name))
End If
Return oSession
End Function

Shafqat Ahmed

A few comments first. The session does not look like an HttpSession, let me assume that it is a custom session object. Please explain what you are trying to achive by converting "Thread.CurrentPrincipal.Identity.Name"
to an integer which should fail at times and which can even be empty at times.

If you want to identify a thread with integer then use "AppDomain.GetCurrentThreadId()"
to get the running thread's numeric id.

If you want the username under which the code is running then use
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
string username = "" + wp.Identity.Name;

Also please elaborate the benefit you are expecting by creating a GC handle and then freeing it in the same method when the object that is being set is not null.

When we do an alloc, it means that the GCHandleType is normal which infact will not let the object be garbage collected even if the object does not have any reference. Also when you are doing "System.Runtime.InteropServices.GCHandle.Alloc(Me)"
then why do this "gh.Target = oSession"? You could just call System.Runtime.InteropServices.GCHandle.Alloc(oSession) directly.

Please elaborate a little more about the objective because I am unable understand the reason for using GCHandle

Leon Anderson

Thank you for your time and effort already, I have no problem with the
Thread.CurrentPrincipal.Identity.Name which you mention of, the question is more about using
the pointer reference to object.

I use the GCHandle to make a handle on the newly created Me inside the New() statement, then assign an already created variable (the singleton with the same ID) this is a way (i think) of trying to limit the resources used, by creating a single copy of an object (unless diff. ID selected as per my code) This way it means that I have a singleton object but without using the usual GetInstance() method, I was trying to do this on the New call of my object class.

Someone mentioned trying to use the IClonable , but that method wont work, as I would not be able to assign anything to Me inside the New method using Me = oSession.Clone, else I would have just done Me = oSession.(this gives error)
This is the reason for using the gchandle , as I am assigning the reference of my first object to my newly created object(using its handle).....sounding more like c++, but I figured it might be the only way to get my singleton implementation of an object already being used by many people without breaking their code.

Leon Anderson

Is there anything you could think of about my last comment that would make it possible to do what I need to accomplish, think of it if you will as using C like pointers to point to an object at the time of creation. If the New has a different Id as a parameter, you create a new instance of that class, but if the Id is the same, you pass the one already being help in a static variable to the pointer of the New statement (the Me)

The comments to this entry are closed.