CObjectSystem / COS

C Object System: a framework that brings C to the level of other high level programming languages and beyond

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

auto objects and cos_init()

bkerin opened this issue · comments

It took me some time to realize why this program seg faults:

OBJ anint = aInt(42);
fprintf (stderr, "int: %i\n", gint(anint));

This one is annoying because of it gets masked by other code which does cos_init(). Always doing an explicit cos_init() is the safe thing but the OOPSLA paper doesn't mention this and the examples don't show it. This

It seems to me that in debug mode (or something) the gint() (and the like) might be able to trigger a
check of cos_symbol.c/init_done static (through an interface function). Or maybe that
would be regarded as too slow even for debug mode, I don't know.

This code does give a nice explanation:

OBJ anint = aInt(42);
cos_init();
fprintf (stderr, "int: %i\n", gint(anint));

COS says:

COS-Abort[8876]: invalid class id 671088640 - dynamic object already destroyed or automatic object used before COS initialization

I'm really just going through and documenting all the trip-ups I make as I get started
with COS.

In principle, one should not start with an automatic object, the only case where this could happen. It is best to always start by creating an AutoRelease pool, which will automatically initialize COS when sending the message.