Buy any Easy Native Extensions 2nd Edition package and get our $99 iOS + Android ANE Template completely free before the end of June 2015.
- step-by-step guide to making your iOS extension in under an hour
- library for data conversion between ActionScript and native code
- tutorials
- infographics
- code included
The Extension Initializer and Finalizer are the entry and exit points to your native extension. They are also two of the ingredients that make your extension known to the world and make it distinguishable from other extensions in an app. This is why they need to have unique names. Their signatures however need to be exactly as prescribed by AIR. Today we look at what these are in the AIR C API.
Extension Initializer in C – FREInitializer
The C version of the Extension Initializer is a function with the following signature:
1 2 3 |
typedef void (*FREInitializer)( void** extDataToSet, FREContextInitializer* ctxInitializerToSet, FREContextFinalizer* contextFinalizerToSet ); |
It is called when your extension is first loaded by an app and its role is to set the following:
- extDataToSet – a pointer to a pointer to data that the extension may need. It is allocated in FREInitializer. It can also be NULL if you don’t need extension-specific global data.
- ctxInitializerToSet – a pointer to another function, the Extension Context Initializer, which will be called when your extension instantiates an Extension Context.
- contextFinalizerToSet – a pointer to a function, the Extension Context Finalizer, which will be called when your Extension Context is destroyed.
Here is an example implementation of a FREInitializer:
1 2 3 4 5 6 7 8 |
void CameraExtensionInitializer( void** extDataToSet, FREContextInitializer* ctxInitializerToSet, FREContextFinalizer* ctxFinalizerToSet ) { *extDataToSet = NULL; *ctxInitializerToSet = &CameraExtensionContextInitializer; *ctxFinalizerToSet = &CameraExtensionContextFinalizer; } |
Extension Finalizer in C – FREFinalizer
This is the C signature of the Extension Finalizer function:
1 |
typedef void (*FREFinalizer)( void* extData ); |
You may notice a familiar argument here:
- extData is a pointer to the data that was allocated in FREInitializer, in other words it’s the same as extDataToSet*. FREFinalizer’s role is to tidy up after the extension, so any data that FREInitializer may have allocated is deallocated here. Or, if no data was allocated and extDataToSet was left NULL, nothing is done with extData.
Here is what the respective implementation of FREFinalizer would look like:
1 2 3 4 5 |
void CameraExtensionFinalizer( void* extData ) { // Free any extension-specific data or resources that may have been reserved return; } |
What’s next?
Next we will look at what’s an Extension Context and what’s an Extension Context Initializer and Finalizer.
Then we will see in what order all of these initializers and finalizers are called when your ANE is loaded and when it’s unloaded.
We will also have a look at the Java versions of the Extension Initializer and Finalizer.
Over to you
Do you have a convention for naming your Extension Initializer and Finalizer? What is it? Tell us in the comments below.
Leave a Reply