[Gambas-devel] Early auto-instance

Benoît Minisini gambas at ...1...
Sat May 12 22:38:50 CEST 2012


Le 12/05/2012 21:14, tobi a écrit :
> On Sat, 12 May 2012, tobi wrote:
>> Hi,
>>
>> I have a compilation unit that provides a function which in turn depends on the state of an object.
>> The default object is the auto-instance of a class (it is just a simple buffered output) but the
>> auto-instance of a class seems to be created on-the-fly when first requested. Now this auto-instance
>> may not be created normally before I need to take actions depending on its state (call the exported
>> function by another piece of code)... I thought of having
>>
>> GB.AutoCreate(GB.FindClass("Screen"), 0);
>>
>> called from within the GB_INIT() of my component to ensure that there exists a valid object. The
>> program works now but I get a segfault:
>>
>> Program received signal SIGSEGV, Segmentation fault.
>> MEMORY_clear_cache () at ../share/gb_alloc_temp.h:96
>> 96                              next = *((void **)ptr);
>> (gdb) print ptr
>> $1 = (void *) 0x123
>>
>> when the program is going to exit. Is there somehting, I should keep in mind when manually creating
>> this auto-instance?

GB_INIT() is called before the component classes are registered, so 
GB.FindClass("Screen") should return NULL. So this is not the good place 
for that!

The goal of having auto-creatable class is the ability of using the 
class name as the default object of a class. That's all.

>>
>> Another point: This is provisional only; I will have to rewrite it anyway when I make Screen a true
>> creatable class.

GB_AUTO_CREATABLE() is for true creatable class, even if you add 
GB_NOT_CREATABLE(), that just means that we cannot use the New operator 
to instantiate the class in code. But the class keeps being creatable 
internally.

A class that is not creatable class has GB_VIRTUAL(), or a size of 0 in 
the GB_DECLARE() statement and only static methods.

>> So there is my next question regarding coding style in gambas: Is it common to save
>> a pointer to another gambas object (the parent object) when creating? This would solve the
>> auto-instance problem as my Screen class (which is to auto-create earlily) is reference during
>> instanciation of the Window object and the latter is the object which calls the function in question
>> (which depends on the active Screen class to enable buffered output on screen).

Of course you can have child/parent relationship through object 
pointers, provided that you use GB.Ref() and GB.Unref() effectively.

But why do you need to instanciate the default Screen class object 
early? It seems weird to me...

>> That sounds tricky,
>> so I wanted to ask if it is considered good style in gambas or if there are other ways to do it. The
>> place I knew to look for was the gb.qt4 component which delegates the parent object stuff to qt
>> itself...
>>
>> Regards,
>> Tobi
>>
>
> Oops!
> I also auto-create a Window object so the parent is to be optional... This solution wouldn't prevent
> me from manually auto-creating the Screen class. I tend to remove the GB_AUTO_CREATABLE() statement
> from both descriptions :/
>

-- 
Benoît Minisini




More information about the Devel mailing list