Article Preview
Buy Now
| Print: | |
| PDF: |
Instant Cocoa
Memory Management in Cocoa
Issue: 1.6 (June/July 2003)
Author: Colin Cornaby
Author Bio: Colin Cornaby is an OS X developer. He is the author of Duality 4, a theme changer written in Cocoa for Mac OS X, and the ThemeKit development framework, for writing theme applications in Cocoa.
Article Description: No description available.
Article Length (in bytes): 4,982
Starting Page Number: 43
RBD Number: 1623
Resource File(s): None
Related Link(s): None
Known Limitations: None
Excerpt of article text...
Memory Management in Cocoa
One of the scarier aspects of Cocoa can be memory management. For programmers who already work in languages that require memory management to be programmed, Cocoa's memory management is very easy to learn. However, programmers who write code in a language in which memory management is handled for them (such as Java or REALbasic) have a bit to learn.
Let's take an example using NSImage. All you need to know about NSImage for now is that it is a variable type that holds an image. In this example our NSImage will be named "foo". Before we can put an image into "foo", we need to create a space in memory. We then need to initialize that space, or in other words, get the space in memory ready to hold an NSImage. To do this we can use the "alloc" and "init" methods. Alloc will create space in memory for the image. Init prepares that space to hold an image. Here is how you might write some code to alloc and init "foo".
NSImage *foo;
foo = [[NSImage alloc] init];
First off, notice we are using two sets of brackets. This isn't too uncommon, but this is the first time I have shown such an example. We are simply allocating an NSImage in one set of brackets, and initializing the result in the second set. Also notice we are initializing after the allocation. You can't initialize something that doesn't exist, of course.
...End of Excerpt. Please purchase the magazine to read the full article.
Article copyrighted by REALbasic Developer magazine. All rights reserved.
|







