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