somUninit
somUninit This method un-initializes the receiving object. Designed to be overridden by class implementors. Not normally invoked directly by object clients.
Note: For backward compatibility, this method does not take an Environment parameter.
- Original Class
- SOMObject
Syntax
void somUninit (SOMObject receiver)
Parameters
- receiver (SOMObject)
- A pointer to the object to be un-initialized.
Return Code
rc (void)
Remarks
The somUninit method performs the inverse of object initialization. Class implementors that introduce instance data that points to allocated storage should override somUninit so allocated storage can be freed when an object is freed.
This method is called automatically by somFree to clean up anything necessary (such as extra storage dynamically allocated to the object) before somFree releases the storage allocated to the object itself.
Code responsible for freeing an object must first know that there will be no further references to this object. Once this is known, this code would normally invoke somFree (which calls somUninit). In cases where somRenew was used to create an object instance, however, somFree cannot be called (e.g., the storage containing the object may simply be a location on the stack), and in this case, somUninit must be called explicitly.
When overriding this method, always call the parent-class versions of this method after doing your own un-initialization. Furthermore, just as with somInit, because your method may be called multiple times (due to multiple inheritance), you should zero out references to memory that is freed, and check for zeros before freeing memory and calling the parent methods.
Example Code
Following is the implementation for a class Animal that introduces an attribute sound of type string and overrides somInit and somUninit, along with a main program that creates and then frees an instance of class Animal.
#define Animal_Class_Source #include <animal.ih> #include <string.h> SOM_Scope void SOMLINK somInit (Animal somSelf) { AnimalData *somThis = AnimalGetData (somSelf); Environment *ev = somGetGlobalEnvironment(); Animal_parents_somInit (somSelf); if (!__get_sound(somSelf, ev)) { __set_sound(somSelf, ev, SOMMalloc(100)); strcpy (__get_sound(somSelf, ev), "Unknown Noise"); somPrintf ("New Animal Initialized\n"); } } SOM_Scope void SOMLINK somUninit (Animal somSelf) { AnimalData *somThis = AnimalGetData (somSelf); Environment *ev = somGetGlobalEnvironment(); if (__get_sound(somSelf, ev)) { SOMFree(__get_sound(somSelf, ev); __set_sound(somSelf, ev, (char*)0); somPrintf ("Animal Uninitialized\n"); Animal_parents_somUninit (somSelf); } } /* main program */ #include <animal.h> void main() { Animal myAnimal; myAnimal = AnimalNew (); _somFree (myAnimal); } /* Program output: New Animal Initialized Animal Uninitialized */