When creating C modules that are wrappers around 3rd party libraries, you often have to allocate some struct when the object is created, and free it when the object is destroyed.
What is the right way to do this? Assuming we have: CVAR struct foo_struct *foo;
Do I set foo = NULL in INIT, then foo_init(foo) in create? Or should I just be doing the foo_init(foo) in INIT? And when freeing it later, should I be doing foo_destruct(foo) in EXIT or in destroy?
Also, when using CMODs INHERIT, do INIT and EXIT get inherited as well, or just PIKEFUNs PIKEVARs and CVARs?
Thanks Adam
Do I set foo = NULL in INIT, then foo_init(foo) in create?
Yes. For robustness, you should also check in create() if foo is still NULL before allocating anything. Just to guard against create() being called twice on the same object.
Or should I just be doing the foo_init(foo) in INIT?
Generally, no. INIT should just set the storage to a consistent state.
And when freeing it later, should I be doing foo_destruct(foo) in EXIT or in destroy?
In EXIT.
Also, when using CMODs INHERIT, do INIT and EXIT get inherited as well, or just PIKEFUNs PIKEVARs and CVARs?
INIT and EXIT callbacks are inherited, otherwise things would break horribly if you inherit a C class from user Pike code.
pike-devel@lists.lysator.liu.se