1<html lang="en"> 2<head> 3<title>What you can and what you cannot do in +load - 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="Executing-code-before-main.html#Executing-code-before-main" title="Executing code before main"> 9<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 10<!-- 11Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 121998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 132010 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="What-you-can-and-what-you-cannot-do-in-+load"></a> 49<a name="What-you-can-and-what-you-cannot-do-in-_002bload"></a> 50<p> 51Up: <a rel="up" accesskey="u" href="Executing-code-before-main.html#Executing-code-before-main">Executing code before main</a> 52<hr> 53</div> 54 55<h4 class="subsection">8.2.1 What you can and what you cannot do in <code>+load</code></h4> 56 57<p><code>+load</code> is to be used only as a last resort. Because it is 58executed very early, most of the Objective-C runtime machinery will 59not be ready when <code>+load</code> is executed; hence <code>+load</code> works 60best for executing C code that is independent on the Objective-C 61runtime. 62 63 <p>The <code>+load</code> implementation in the GNU runtime guarantees you the 64following things: 65 66 <ul> 67<li>you can write whatever C code you like; 68 69 <li>you can allocate and send messages to objects whose class is implemented 70in the same file; 71 72 <li>the <code>+load</code> implementation of all super classes of a class are 73executed before the <code>+load</code> of that class is executed; 74 75 <li>the <code>+load</code> implementation of a class is executed before the 76<code>+load</code> implementation of any category. 77 78 </ul> 79 80 <p>In particular, the following things, even if they can work in a 81particular case, are not guaranteed: 82 83 <ul> 84<li>allocation of or sending messages to arbitrary objects; 85 86 <li>allocation of or sending messages to objects whose classes have a 87category implemented in the same file; 88 89 <li>sending messages to Objective-C constant strings (<code>@"this is a 90constant string"</code>); 91 92 </ul> 93 94 <p>You should make no assumptions about receiving <code>+load</code> in sibling 95classes when you write <code>+load</code> of a class. The order in which 96sibling classes receive <code>+load</code> is not guaranteed. 97 98 <p>The order in which <code>+load</code> and <code>+initialize</code> are called could 99be problematic if this matters. If you don't allocate objects inside 100<code>+load</code>, it is guaranteed that <code>+load</code> is called before 101<code>+initialize</code>. If you create an object inside <code>+load</code> the 102<code>+initialize</code> method of object's class is invoked even if 103<code>+load</code> was not invoked. Note if you explicitly call <code>+load</code> 104on a class, <code>+initialize</code> will be called first. To avoid possible 105problems try to implement only one of these methods. 106 107 <p>The <code>+load</code> method is also invoked when a bundle is dynamically 108loaded into your running program. This happens automatically without any 109intervening operation from you. When you write bundles and you need to 110write <code>+load</code> you can safely create and send messages to objects whose 111classes already exist in the running program. The same restrictions as 112above apply to classes defined in bundle. 113 114 </body></html> 115 116