Hi,
thanks for replay.
I expect pointer not to be collected, because it "lives" in variable h.
What's wrong with my code?
The reference counter.
...
If you need the exact technical
answer what was wrong in your code then this is hacked version
of your code to work properly. Please never try to make sth like
that because it may not work in future HVM versions.
HB_FUNC( MYFUNC )
{
void* ptr = hb_parptrGC( hb_fctx_destructor, 1 );
if( ! ptr )
ptr = hb_gcAlloc( 4, hb_fctx_destructor );
else
hb_gcRefInc( ptr );
hb_retptrGC( ptr );
}
That's ***EXACTLY*** the same code I found in 15min after I'd written a
reduced sample of my problem. But it's hacky, that's why I've wrote a
letter to mailing list.
The goal was to write function like:
FUNC MyFunc( param )
IF param == NIL; param := {some array}
ENDIF
RETURN param
so the code using hb_parptrGC()/hb_retptrGC() was "natural".
Now your code make sth like:
HB_FUNC( MYFUNC )
{
char * str = hb_parc( 1 );
if( ! str )
str = ( char * ) hb_xgrab( 4 );
hb_retc_buffer( str );
}
Thanks for comparison. It will help me to remember the idea.
Multiple putting the same pointer in
GC pointer item is not safe. If you think that it should be
safe then I can add such functionality but in such case IMHO
I should also update hb_itemPutCPtr() to be safe for multiple
putting the same string pointer.
Perhaps it's a big change, and it seems we manage OK with string
pointers (well, in most times...). So, we just need to get used to
pointers also.
But maybe it's related to your pending changes of string pointers and
you have a feeling it should be changed to be safe. Then, please, do the
change.
But you can also change your code:
...
HB_FUNC( MYFUNC )
{
PHB_ITEM pItem = NULL;
void* ptr = hb_parptrGC( hb_fctx_destructor, 1 );
if( ! ptr )
{
ptr = hb_gcAlloc( 4, hb_fctx_destructor );
pItem = hb_itemPutPtrGC( NULL, ptr );
/* now ptr is safe and you do not have to worry that
* it can be removed by some code inside <other code>
* which may activate GC
*/
}
/* <other code> */
if( pItem )
hb_itemReturnRelease( pItem );
else
hb_itemReturn( hb_param( 1, HB_IT_ANY ) );
}
This code is OK for me, thanks. I'm also surprised, that You've answer
one more question, I haven't asked for!!! The question was: after
hb_gcAlloc() no item is pointing to pointer, could it be freed by GC?
Best regards,
Mindaugas
_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour