Important: The information in this document is obsolete and should not be used for new development.
Allocating Memory for and Disposing of Global Data
You can use theNewMessageGlobalsfunction to request and allocate memory for globals. You should only call this function while your application is performing a message override.You should always initialize your global data from a function other than the one in which you call the
NewMessageGlobalsfunction. Otherwise, your development environment may generate code with bad data references.Listing 6-1 gives an example of using the
NewMessageGlobalsfunction to create an A5 world from an MPW programming environment.Listing 6-1 Creating an A5 world for global data
gxShape gMyShape; Handle gMyHandle; OSErr MyInitGlobalData() { OSErr err; gMyShape = nil; gMyHandle = TempNewHandle(1024, &err); return err; } OSErr MyInitialize() { OSErr err; /* Create an A5 world, and initialize the global data. */ err = NewMessageGlobals(A5Size(), A5Init); if (!err) err = MyInitGlobalData(); return err; }TheMyInitalizefunction is the override for theGXInitializemessage. TheMyInitializefunction first sets up an A5 world, as required if an extension is going to use global data. In this case the global data is theMyShapestructure. Once you create the A5 world by calling theNewMessageGlobalsfunction, your global data will be valid whenever your printing extension or printer driver is called. Once theNewMessageGlobalsfunction has been called, the extension or driver can initialize its global data. In this example, the code uses a function calledMyInitGlobalDatato do this.If you have allocated memory for your globals using the
NewMessageGlobalsfunction, you must use theDisposeMessageGlobalsfunction to dispose of the globals and deallocate their memory blocks when they are no longer needed.Note that
DisposeMessageGlobalsdoes not dispose of data and handles. These must be disposed of by your code. First, you deallocate any memory that you have allocated and then let QuickDraw GX deallocate memory that it has allocated for your global data.Listing 6-2 shows how to dispose of global data and deallocate the memory that was allocated in Listing 6-1.
Listing 6-2 Disposing of global data and deallocating memory
OSErr MyShutDown() {/* Dispose of our global data */ if (gMyHandle != nil) DisposHandle(GMyHandle); /* dispose of the A5 world that was created in MyInitialize */ DisposeMessageGlobals(); return noErr; }TheNewMessageGlobalsfunction is described on page 6-17. TheDisposeMessageGlobalsfunction is described on page 6-18.Global data is discussed in the section "Global Data Storage for Printing Extensions and Printer Drivers" beginning on page 6-7.