On Thu, 08 Oct 2009, Pritpal Bedi wrote:
Hi,
> > In order to let GC collect these pointer automatically
> > you need to use hb_retptrGC(), along with a callback
> > to the freeing.
> So, I tried like this:
>
> static HB_GARBAGE_FUNC( QMessageBox_release )
> {
> void ** ph = ( void ** ) Cargo;
>
> /* Check if pointer is not NULL to avoid multiple freeing */
> if( ph && * ph )
> {
> /* Destroy the object */
> delete ( ( QMessageBox * ) ph );
> /* set pointer to NULL to avoid multiple freeing */
> * ph = NULL;
> }
> }
>
> static QMessageBox* xhbqt_par_QMessageBox( int iParam )
> {
> void ** ph = ( void ** ) hb_parptrGC( QMessageBox_release, iParam );
> return ph ? ( QMessageBox * ) ph : NULL;
> }
Here is mistake. The last line should be changed to:
return ph ? ( QMessageBox * ) * ph : NULL;
> HB_FUNC( QT_QMESSAGEBOX )
> {
> void ** ph = ( void ** ) hb_gcAlloc( sizeof( QMessageBox ),
> QMessageBox_release );
> * ph = new QMessageBox();
> hb_retptrGC( ( QMessageBox * ) ph );
> }
>
> /*
> * DESTRUCTOR
> */
> HB_FUNC( QT_QMESSAGEBOX_DESTROY )
> {
> void ** ph = ( void ** ) hb_parptrGC( QMessageBox_release, 1 );
> if( ph && * ph )
> {
> delete ( ( QMessageBox * ) ph );
> * ph = NULL;
> }
> }
Very good. Please only remember that QT_QMESSAGEBOX_DESTROY is optional.
You do not have to implement this function because destructor will be
automatically activated when the last .prg item holding the pointer
to QMessageBox will be released (cleared or overwritten).
In some cases you may also need function DETACH which will be used for
widgets attached to some other QT widgets so they will be released
automatically with parent objects and should not be release by our GC.
I.e.:
void xhbqt_detach_QMessageBox( PHB_ITEM pItem )
{
void ** ph = ( void ** ) hb_itemGetPtrGC( pItem, QMessageBox_release );
if( ph && *ph )
*ph = NULL;
}
In most of cases such function should be called by C code which attach
widget to parent object but if you really need it then you can create
.prg wrapper two:
HB_FUNC( QT_QMESSAGEBOX_DETACH )
{
xhbqt_detach_QMessageBox( hb_param( 1, HB_IT_POINTER ) );
}
Please only remember that after detaching GC will not free the object
automatically and it has to be freed in other way, i.e. by parent object.
best regards,
Przemek
_______________________________________________
Harbour mailing list
[email protected]
http://lists.harbour-project.org/mailman/listinfo/harbour