Hello Przemek,

I have a memory table set in C++ that holds pairs of HBObject <-> C+ +Object,
one can get the C++Object associated to an HBObject and viceversa:

  HBObject    C++Object
--------------------------
| HBObjectA | CppObjectA |
| HBObjectB | CppObjectB |
| HBObjectC | CppObjectC |
|    ...    |     ...    |
--------------------------


HBObject column has the values of: HBObject->item.asArray.value
C++Object column has the address of each CppObject (*CppObject).

Each pair relation is added when a new HBObject calls his New() method
(which is in C++) and the function "AddObjsToTable" does the job:


<start source c++>
HB_FUNC( HBOBJECT_NEW )
{
  PHB_ITEM pSelf = hb_stackSelfItem();

  CPPObject *cppObject;

  ...
  /* process parameters and creates the CPPObject */

  cppObject = new CPPObject( /* params */ );
  ...

  AddObjsToTable( pSelf, cppObject );

  hb_itemReturn( pSelf );
}
<end source c++>


Currently the "AddObjsToTable" function has the form:


<start source c++>
void AddObjsToTable( PHB_ITEM pSelf, CPPObject * cppObject )
{
   PHB_ITEM pCopyOfSelf = hb_itemNew( pSelf );
   PHB_BASEARRAY pBaseArray = pCopyOfSelf->item.asArray.value;

   hashMap_HB_To_Cpp[ pBaseArray ] = cppObject;
   hashMap_Cpp_To_HB[ cppObject ] = pCopyOfSelf;
}
<end source c++>


The problem with the above construct, is that it blocks the ability to
automatically destroy the HB Object if their container var is out of
scope in prg code. This is because the line in "AddObjsToTable":

   PHB_ITEM pCopyOfSelf = hb_itemNew( pSelf );

But I'm wondering if I can safely use the following construct instead of
the above line:

   PHB_ITEM pCopyOfSelf = hb_itemNew( pSelf );
   hb_gcRefDec( pCopyOfSelf->item.asArray.value );

In this way, when in prg code the container var for the object runs out
of scope (or is cleared in any way), then the Object is destroyed
(i.e. calling his destructor method), and there is only left to release
the item stored in the hash map with:

   PHB_ITEM pCopyOfSelf = hashMap_Cpp_To_HB[ cppObject ];
   pCopyOfSelf->type = HB_IT_NIL;
   hb_itemRelease( pWxh_Item->pSelf );


Resumming, I need to create a kind of "fake" Object that allows to create prg code were if an var holding an Object is running out of scope then it is destroyed by the GC, and at the same time, it allows to have a "fake copy" of the HB object at C++ level to be able to send messages and return Self.


In advance, thanks for any help, hint or advice, provided.

best regards,

Teo

PS: Sure I made some errors redacting this
but when wife calls to eat, she is unstoppable.

_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to