Garbage-Collection.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Garbage Collection</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Garbage Collection">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Garbage Collection">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Objective_002dC.html#Objective_002dC" rel="up" title="Objective-C">
  30. <link href="Constant-string-objects.html#Constant-string-objects" rel="next" title="Constant string objects">
  31. <link href="Method-signatures.html#Method-signatures" rel="prev" title="Method signatures">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Garbage-Collection"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Constant-string-objects.html#Constant-string-objects" accesskey="n" rel="next">Constant string objects</a>, Previous: <a href="Type-encoding.html#Type-encoding" accesskey="p" rel="prev">Type encoding</a>, Up: <a href="Objective_002dC.html#Objective_002dC" accesskey="u" rel="up">Objective-C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Garbage-Collection-1"></a>
  69. <h3 class="section">8.4 Garbage Collection</h3>
  70. <p>This section is specific for the GNU Objective-C runtime. If you are
  71. using a different runtime, you can skip it.
  72. </p>
  73. <p>Support for garbage collection with the GNU runtime has been added by
  74. using a powerful conservative garbage collector, known as the
  75. Boehm-Demers-Weiser conservative garbage collector.
  76. </p>
  77. <p>To enable the support for it you have to configure the compiler using
  78. an additional argument, <samp><span class="nolinebreak">--enable-objc-gc</span></samp><!-- /@w -->. This will
  79. build the boehm-gc library, and build an additional runtime library
  80. which has several enhancements to support the garbage collector. The
  81. new library has a new name, <samp>libobjc_gc.a</samp> to not conflict with
  82. the non-garbage-collected library.
  83. </p>
  84. <p>When the garbage collector is used, the objects are allocated using the
  85. so-called typed memory allocation mechanism available in the
  86. Boehm-Demers-Weiser collector. This mode requires precise information on
  87. where pointers are located inside objects. This information is computed
  88. once per class, immediately after the class has been initialized.
  89. </p>
  90. <p>There is a new runtime function <code>class_ivar_set_gcinvisible()</code>
  91. which can be used to declare a so-called <em>weak pointer</em>
  92. reference. Such a pointer is basically hidden for the garbage collector;
  93. this can be useful in certain situations, especially when you want to
  94. keep track of the allocated objects, yet allow them to be
  95. collected. This kind of pointers can only be members of objects, you
  96. cannot declare a global pointer as a weak reference. Every type which is
  97. a pointer type can be declared a weak pointer, including <code>id</code>,
  98. <code>Class</code> and <code>SEL</code>.
  99. </p>
  100. <p>Here is an example of how to use this feature. Suppose you want to
  101. implement a class whose instances hold a weak pointer reference; the
  102. following class does this:
  103. </p>
  104. <div class="smallexample">
  105. <pre class="smallexample">
  106. @interface WeakPointer : Object
  107. {
  108. const void* weakPointer;
  109. }
  110. - initWithPointer:(const void*)p;
  111. - (const void*)weakPointer;
  112. @end
  113. @implementation WeakPointer
  114. + (void)initialize
  115. {
  116. if (self == objc_lookUpClass (&quot;WeakPointer&quot;))
  117. class_ivar_set_gcinvisible (self, &quot;weakPointer&quot;, YES);
  118. }
  119. - initWithPointer:(const void*)p
  120. {
  121. weakPointer = p;
  122. return self;
  123. }
  124. - (const void*)weakPointer
  125. {
  126. return weakPointer;
  127. }
  128. @end
  129. </pre></div>
  130. <p>Weak pointers are supported through a new type character specifier
  131. represented by the &lsquo;<samp>!</samp>&rsquo; character. The
  132. <code>class_ivar_set_gcinvisible()</code> function adds or removes this
  133. specifier to the string type description of the instance variable named
  134. as argument.
  135. </p>
  136. <hr>
  137. <div class="header">
  138. <p>
  139. Next: <a href="Constant-string-objects.html#Constant-string-objects" accesskey="n" rel="next">Constant string objects</a>, Previous: <a href="Type-encoding.html#Type-encoding" accesskey="p" rel="prev">Type encoding</a>, Up: <a href="Objective_002dC.html#Objective_002dC" accesskey="u" rel="up">Objective-C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  140. </div>
  141. </body>
  142. </html>