3 <p>GC handles are wrappers that are used to keep references to
4 managed objects in the unmanaged space and preventing the
5 object from being disposed.
7 <p>These are the C equivalents of the <tt>System.GCHandle</tt>
10 <p>There are two kinds of GCHandles that can be created:
13 <li>Handles to objects (use <tt><a
14 href="api:mono_gchandle_new">mono_gchandle_new</a></tt>).
16 <li>Weak handles to objects (use <tt><a
17 href="api:mono_gchandle_new_weakref">mono_gchandle_new_weakref</a></tt>).
18 Weak handles can have the objects reclaimed by the
23 <p>To retrieve the target address of an object pointed to by a
24 <tt>GCHandle</tt> you should use
25 <tt>mono_gchandle_get_target</tt>.
27 <p>For example, consider the following C code:
29 static MonoObject* o = NULL;
32 <p>The object in `o' will *NOT* be scanned.
34 <p>If you need to store an object in a C variable and prevent
35 it from being collected, you need to acquire a GC handle for
39 guint32 handle = mono_gchandle_new (my_object, TRUE);
42 <p>TRUE means the object will be pinned, so it won't move in
43 memory when we'll use a moving GC. You can access the
44 MonoObject* referenced by a handle with:
47 MonoObject* obj = mono_gchandle_get_target (handle);
50 <p>When you don't need the handle anymore you need to call:
53 mono_gchandle_free (handle);
56 <p>Note that if you assign a new object to the C var, you need
57 to get a new handle, it's not enough to store a new object in
60 <p>So code that looked like this:
63 static MonoObject* o = NULL;
65 o = mono_object_new (...);
68 /* when done to allow the GC to collect o */
72 <p>should now be changed to:
75 static guint32 o_handle;
77 MonoObject *o = mono_object_new (...);
78 o_handle = mono_gchandle_new (o, TRUE);
79 /* use o or mono_gchandle_get_target (o_handle) */
81 /* when done to allow the GC to collect o */
82 mono_gchandle_free (o_handle);
85 <h4><a name="api:mono_gchandle_new">mono_gchandle_new</a></h4>
86 <h4><a name="api:mono_gchandle_new_weakref">mono_gchandle_new_weakref</a></h4>
87 <h4><a name="api:mono_gchandle_get_target">mono_gchandle_get_target</a></h4>
88 <h4><a name="api:mono_gchandle_is_in_domain">mono_gchandle_is_in_domain</a></h4>
89 <h4><a name="api:mono_gchandle_free">mono_gchandle_free</a></h4>