summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/what-you-can-and-what-you-cannot-do-in-_002bload.html
blob: 1863ed917897f566068446695eeff15f8c7e7f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<div class="subsection-level-extent" id="What-you-can-and-what-you-cannot-do-in-_002bload"> <div class="nav-panel"> <p> Up: <a href="executing-code-before-main" accesskey="u" rel="up"><code class="code">+load</code>: Executing Code before <code class="code">main</code></a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div>  <h1 class="subsection" id="What-You-Can-and-Cannot-Do-in-_002bload"><span>8.2.1 What You Can and Cannot Do in +load<a class="copiable-link" href="#What-You-Can-and-Cannot-Do-in-_002bload"> ¶</a></span></h1> <p><code class="code">+load</code> is to be used only as a last resort. Because it is executed very early, most of the Objective-C runtime machinery will not be ready when <code class="code">+load</code> is executed; hence <code class="code">+load</code> works best for executing C code that is independent on the Objective-C runtime. </p> <p>The <code class="code">+load</code> implementation in the GNU runtime guarantees you the following things: </p> <ul class="itemize mark-bullet"> <li>you can write whatever C code you like; </li>
<li>you can allocate and send messages to objects whose class is implemented in the same file; </li>
<li>the <code class="code">+load</code> implementation of all super classes of a class are executed before the <code class="code">+load</code> of that class is executed; </li>
<li>the <code class="code">+load</code> implementation of a class is executed before the <code class="code">+load</code> implementation of any category. </li>
</ul> <p>In particular, the following things, even if they can work in a particular case, are not guaranteed: </p> <ul class="itemize mark-bullet"> <li>allocation of or sending messages to arbitrary objects; </li>
<li>allocation of or sending messages to objects whose classes have a category implemented in the same file; </li>
<li>sending messages to Objective-C constant strings (<code class="code">@"this is a
constant string"</code>); </li>
</ul> <p>You should make no assumptions about receiving <code class="code">+load</code> in sibling classes when you write <code class="code">+load</code> of a class. The order in which sibling classes receive <code class="code">+load</code> is not guaranteed. </p> <p>The order in which <code class="code">+load</code> and <code class="code">+initialize</code> are called could be problematic if this matters. If you don’t allocate objects inside <code class="code">+load</code>, it is guaranteed that <code class="code">+load</code> is called before <code class="code">+initialize</code>. If you create an object inside <code class="code">+load</code> the <code class="code">+initialize</code> method of object’s class is invoked even if <code class="code">+load</code> was not invoked. Note if you explicitly call <code class="code">+load</code> on a class, <code class="code">+initialize</code> will be called first. To avoid possible problems try to implement only one of these methods. </p> <p>The <code class="code">+load</code> method is also invoked when a bundle is dynamically loaded into your running program. This happens automatically without any intervening operation from you. When you write bundles and you need to write <code class="code">+load</code> you can safely create and send messages to objects whose classes already exist in the running program. The same restrictions as above apply to classes defined in bundle. </p> </div>  <div class="nav-panel"> <p> Up: <a href="executing-code-before-main"><code class="code">+load</code>: Executing Code before <code class="code">main</code></a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
  <p class="_attribution-p">
    &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
    <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/What-you-can-and-what-you-cannot-do-in-_002bload.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/What-you-can-and-what-you-cannot-do-in-_002bload.html</a>
  </p>
</div>