1<html lang="en"> 2<head> 3<title>Garbage Collection - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Objective_002dC.html#Objective_002dC" title="Objective-C"> 9<link rel="prev" href="Type-encoding.html#Type-encoding" title="Type encoding"> 10<link rel="next" href="Constant-string-objects.html#Constant-string-objects" title="Constant string objects"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Garbage-Collection"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Constant-string-objects.html#Constant-string-objects">Constant string objects</a>, 53Previous: <a rel="previous" accesskey="p" href="Type-encoding.html#Type-encoding">Type encoding</a>, 54Up: <a rel="up" accesskey="u" href="Objective_002dC.html#Objective_002dC">Objective-C</a> 55<hr> 56</div> 57 58<h3 class="section">8.4 Garbage Collection</h3> 59 60<p>This section is specific for the GNU Objective-C runtime. If you are 61using a different runtime, you can skip it. 62 63 <p>Support for garbage collection with the GNU runtime has been added by 64using a powerful conservative garbage collector, known as the 65Boehm-Demers-Weiser conservative garbage collector. 66 67 <p>To enable the support for it you have to configure the compiler using 68an additional argument, <samp><span class="option">--enable-objc-gc</span></samp><!-- /@w -->. This will 69build the boehm-gc library, and build an additional runtime library 70which has several enhancements to support the garbage collector. The 71new library has a new name, <samp><span class="file">libobjc_gc.a</span></samp> to not conflict with 72the non-garbage-collected library. 73 74 <p>When the garbage collector is used, the objects are allocated using the 75so-called typed memory allocation mechanism available in the 76Boehm-Demers-Weiser collector. This mode requires precise information on 77where pointers are located inside objects. This information is computed 78once per class, immediately after the class has been initialized. 79 80 <p>There is a new runtime function <code>class_ivar_set_gcinvisible()</code> 81which can be used to declare a so-called <dfn>weak pointer</dfn> 82reference. Such a pointer is basically hidden for the garbage collector; 83this can be useful in certain situations, especially when you want to 84keep track of the allocated objects, yet allow them to be 85collected. This kind of pointers can only be members of objects, you 86cannot declare a global pointer as a weak reference. Every type which is 87a pointer type can be declared a weak pointer, including <code>id</code>, 88<code>Class</code> and <code>SEL</code>. 89 90 <p>Here is an example of how to use this feature. Suppose you want to 91implement a class whose instances hold a weak pointer reference; the 92following class does this: 93 94<pre class="smallexample"> 95 @interface WeakPointer : Object 96 { 97 const void* weakPointer; 98 } 99 100 - initWithPointer:(const void*)p; 101 - (const void*)weakPointer; 102 @end 103 104 105 @implementation WeakPointer 106 107 + (void)initialize 108 { 109 class_ivar_set_gcinvisible (self, "weakPointer", YES); 110 } 111 112 - initWithPointer:(const void*)p 113 { 114 weakPointer = p; 115 return self; 116 } 117 118 - (const void*)weakPointer 119 { 120 return weakPointer; 121 } 122 123 @end 124 125</pre> 126 <p>Weak pointers are supported through a new type character specifier 127represented by the ‘<samp><span class="samp">!</span></samp>’ character. The 128<code>class_ivar_set_gcinvisible()</code> function adds or removes this 129specifier to the string type description of the instance variable named 130as argument. 131 132<!-- ========================================================================= --> 133 </body></html> 134 135