On Sat, 02 Feb 2008, Mindaugas Kavaliauskas wrote: > Hi, > I expect pointer not to be collected, because it "lives" in variable h. > What's wrong with my code?
The reference counter. 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. 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 ); } It's not safe for the same reason as your example which operates on pointer items. If it's important then I can make both safe. 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 ) ); } and it will work as you want. 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 ); } best regards, Przemek _______________________________________________ Harbour mailing list Harbour@harbour-project.org http://lists.harbour-project.org/mailman/listinfo/harbour