Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualAndreas Jonsson

Posted 14 September 2013 - 04:09 PM

Hi Wracky,

 

If you don't want the delegate to hold a strong reference to the original object, then you'll need to look into implementing a weak-reference container.

 

Luckily for you, with version 2.27.1 weak references is already a reality, so it should be quite easy for you to do cool.png

 

I recommend you keep using the built-in delegates for allowing the script to pass the callback to the application, but instead of storing the received delegate directly, you dissect it and store the object pointer with a weak reference and the method pointer normally.

 

void FuncReceivingDelegate(asIScriptFunction *delegate)
{
   // Get the object pointer from the delegate
   if( delegate->GetFuncType() == asFUNC_DELEGATE )
   {
     void *obj = delegate->GetDelegateObject();
     asIObjectType *objType = delegate->GetDelegateObjectType();
     asIScriptFunction *method = delegate->GetDelegateMethod();
  
     // Create a weak ref to keep the pointer to the object
     CScriptWeakRef *weakRefToDelegatedObject = new CScriptWeakRef(obj, objType);
     
     // Store the weak ref and the method instead of the original delegate
     ...
   }
 
   delegate->Release();
}

 

In case you wish to explore the generic funcdefs option, then you need to know that the generic handle add-on is capable of holding references to funcdefs too.

 

Regards,

Andreas


#1Andreas Jonsson

Posted 14 September 2013 - 04:07 PM

Hi Wracky,

 

If you don't want the delegate to hold a strong reference to the original object, then you'll need to look into implementing a weak-reference container.

 

Luckily for you, with version 2.27.1 weak references is already a reality, so it should be quite easy for you to do cool.png

 

I recommend you keep using the built-in delegates for allowing the script to pass the callback to the application, but instead of storing the received delegate directly, you dissect it and store the object pointer with a weak reference and the method pointer normally.

 

[code=auto:0]

void FuncReceivingDelegate(asIScriptFunction *delegate)

{

   // Get the object pointer from the delegate

   if( delegate->GetFuncType() == asFUNC_DELEGATE )

   {

     void *obj = delegate->GetDelegateObject();

     asIObjectType *objType = delegate->GetDelegateObjectType();

     asIScriptFunction *method = delegate->GetDelegateMethod();

  

     // Create a weak ref to keep the pointer to the object

     CScriptWeakRef *weakRefToDelegatedObject = new CScriptWeakRef(obj, objType);

     

     // Store the weak ref and the method instead of the original delegate

     ...

   }

 

   delegate->Release();

}

 

Regards,

Andreas


PARTNERS