Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 27 Nov 2013
Offline Last Active Jan 17 2014 03:36 AM

Posts I've Made

In Topic: Help with delegates

01 December 2013 - 08:21 PM

Right, that was what I meant to say, but your wording is definitely better than mine.

In Topic: Help with delegates

27 November 2013 - 06:23 PM

The Button should make sure the asIScriptFunction is not released prematurely by calling AddRef on it 


This was it. Thanks for the quick reply.


For those who might struggle with this:


Basically in SetOnClick, I was passing and storing the pointer of the asIScriptFunction in my application.

Then, when I wanted to trigger the event I was just preparing the context of the asIScriptFunction and executing it.


Until recently all my events where asIScriptFunction pointers to global functions of the script, and it seems those are never released until the end of the script execution (I guess because they might be called by the script at any time).

But when I started to store asIScriptFunction pointers to class methods (delegates), I run into the problem of having the asIScriptFunction released between the time it was stored and the time I executed it (unless I declared the delegate in a global or at least class scope, so it remains in memory).


Anyway, I just added AddRef in the SetOnClick method to make sure the asIScriptFunction that is stored won't be released, and everything works ok now.

(I can do TestButton.OnClick = MyEvent(this.TestButtonClick); directly without having to store anything).

In Topic: Help with delegates

27 November 2013 - 04:55 AM

Oh dear...a minute after posting I found the solution.

I declared the delegate as a private member of the script class so it doesn't get freed by the GC (I guess).

Anyway it works, but is there any way to not have to declare all those delegates as private members ?


Just for reference if someone happens to have the same problem, here is the update script. Notice the private delegate of type MyEvent:

class MyScriptClass
	private MyEvent @ClickCallback;

	MyScriptClass(...) {
		Button @TestButton = Button(...);
		@ClickCallback = MyEvent(this.TestButtonClick);
		TestButton.OnClick = @ClickCallback;
		Button @TestButton2 = Button(...);
		TestButton2.OnClick = @TestButtonClick2;
	void TestButtonClick(...) {
		// fails
void TestButtonClick2(...) {
	// works ok
void main()
	MyScriptClass @Test = MyScriptClass(...);