BACKPORT: CMOD module level storage not allocated
Imported from http://bugzilla.roxen.com/bugzilla/show_bug.cgi?id=3911
Reported by BW, Riverweb hww3@riverweb.com
The following CMOD (not within a PIKECLASS) will dump core:
typedef struct
{
char * data;
int started;
} OBJECT_DATA;
CVAR OBJECT_DATA *object_data;
PIKEFUN string foo()
{
if(THIS->object_data->data != NULL)
push_text(THIS->object_data->data);
else
push_int(0);
}
PIKEFUN void create() { }
INIT
{
char * blah = strdup("wahoo!");
OBJECT_DATA * dta =
(OBJECT_DATA*)malloc(sizeof(OBJECT_DATA));
if (!dta)
Pike_error("init_test: Out of memory!\n");
THIS->object_data = dta;
THIS->object_data->data = blah;
}
EXIT
{
if(THIS->object_data)
{
if(THIS->object_data->data != NULL)
free(THIS->object_data->data);
free(THIS->object_data);
}
}
It appears that the top level storage is not allocated. Grubba's suggestion to add the following code to the bottom of the module:
PIKE_MODULE_INIT
{
_storage_offset = ADD_STORAGE(OBJECT_DATA);
INIT;
}
PIKE_MODULE_EXIT
{
EXIT;
}
Solves the problem with the following caveat... the prototype for event_handler() must be provided, as precompile.pike generates code out of order for it to be implicitly defined:
static void event_handler(int ev);