9.1. Memory Management in Carbon

Carbon programs generally use C functions to create and destroy memory. You learned about the malloc and free memory functions in Chapter 6, and Carbon programs can certainly make use of these functions. Carbon provides its own memory-management functions, mostly for historical reasons. CoreFoundation also defines its own memory-management conventions, in part because its objects are reference counted. The following sections describe Carbon and CoreFoundation memory-management functions in more detail.

9.1.1. Carbon Memory Functions

Carbon provides its own functions for allocating memory. Although these functions used to be the native memory functions for the traditional Mac OS 9 operating system, they now eventually call down into the malloc memory functions. The Carbon functions exist now largely for source compatibility with existing code. In addition, the terminology for working with memory has made its way into other parts of the Carbon API. Understanding Carbon memory conventions can help you understand how other older libraries, such as QuickTime, are meant to be used.

9.1.1.1. Carbon Pointers

Carbon manages contiguous chunks of memory using the NewPtr and DisposePtr functions. These functions behave basically the same way as malloc and free. The NewPtr function returns a pointer to memory of a specific size, and DisposePtr allows the system to reclaim that memory. You will see the verbs New and Dispose used frequently within Carbon for ...

Get Beginning Mac OS® X Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.