roxen.lists.pike.general

Subject Author Date
Re: cmod inherit question Henrik_Grubbström <grubba[at]roxen[dot]com> 10-11-2009
On Tue, 10 Nov 2009, Johan Björklund wrote:

> A few things. The best way to know what CMOD does is to examine the generated 
> .c file and find the macros generated there.
>
> The "THIS" macro is defined within each PIKECLASS block to cast the 
> storage-pointer of the "this" object to the struct defining the member 
> variables. That is why you can use -> references in c.
>
> However, on the c-level, all object-type PIKEVARs are defined struct *object.
[...]
>> I know, the structure is not initialized. But where and how to do that? I 
>> tried something like
>>    THIS->Info = HostApiInfo();
> This would be:
>
> THIS->Info = low_clone(HostApiInfo_program);
> call_c_initalizers(THIS->Info);

A more common case would be to use clone_object() (which calls 
call_c_initializers() (and call_pike_initializers()) for you)
instead of low_clone().

> When you use reference types such as objects and strings in CMOD, you must 
> initalize them, otherwise they will blow up in pike:
>
> PIKECLASS HostApiInfo {
>   PIKEVAR string name;
>    INIT{
>     THIS->name = NULL;
>   }
>     EXIT{
>     if (THIS->name != NULL) free_string(THIS->name);
>   }
> }

Actually, PIKEVARs are automatically initialized (to 0, NULL or 0.0 
depending on type) and freed on exit, so both INIT and EXIT in the above 
example are redundant. CVARs on the other hand are NOT initialized 
or freed automatically (unless they've been mapped by hand), and will need 
INIT and EXIT code.

> You should use a pike compiled with the "--with-checker" option. Otherwise 
> you won't see your memory problems.

Not quite; --with-checker adds some code to simplify analysis by C-level 
code checkers (eg valgrind, purify, coverity etc). Most consistency checks 
are added by --with-rtldebug (runtime-level debug).

--
Henrik Grubbström					<grubba[at]roxen.com>
Roxen Internet Software AB