Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Help with delegates


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 christophe-f8   Members   -  Reputation: 107

Like
0Likes
Like

Posted 27 November 2013 - 04:47 AM

Hi,

 

I registered a simple button class which has an event being triggered when it is clicked.

To do so, I registered a funcdef (MyEvent) and a method within the button class (SetOnClick) which stores in my application the asIScriptFunction given in parameter.

This works just fine.

 

Now, I want to create a script class (NOT registered by the application, just a simple script class), which has buttons as private members.

And I want to execute one of the methods of the class when the button is clicked.

I created the method with the same signature as the MyEvent and in the constructor of the class I instantiated a delegate (MyEvent(this.MyMethod)) that I pass as a parameter to the SetOnClick method of the button.

When I break in my app I can see asIScriptFunction->GetFuncType() is asFUNC_DELEGATE, but

most other methods return NULL (GetObjectName, GetObjectType ... GetDeclaration returns "void ::_unnamed_function(...)).

 

I think the problem lays here, but just to explain further: if I just store the asIScriptFunction, when I want to use it

(i.e. when the button is clicked and I want to call the function) all calls on the asIScriptFunction crash (unhandled exception, priviledge instruction ... pretty random), including the calls that were working when it was stored.

 

If it help here is my very simple script (pseudo code):





class MyScriptClass
{
	MyScriptClass(...) {
		Button @TestButton = Button(...);
		TestButton.OnClick = MyEvent(this.TestButtonClick);
		
		Button @TestButton2 = Button(...);
		TestButton2.OnClick = @TestButtonClick2;
	}
	
	void TestButtonClick(...) {
		// fails
	}
}

void TestButtonClick2(...) {
	// works ok
}

void main()
{
	MyScriptClass @Test = MyScriptClass(...);
}

(MyEvent is declared by the application and both TestButtonClick and TestButtonClick2 use the MyEvent signature)


Edited by christophe-f8, 27 November 2013 - 04:55 AM.


Sponsor:

#2 christophe-f8   Members   -  Reputation: 107

Like
0Likes
Like

Posted 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(...);
}


#3 Andreas Jonsson   Moderators   -  Reputation: 3269

Like
0Likes
Like

Posted 27 November 2013 - 07:46 AM

How have you implemented and registered the Button::OnClick  property?

 

The Button should make sure the asIScriptFunction is not released prematurely by calling AddRef on it (or not calling Release, depending on how you've registered the method).


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#4 christophe-f8   Members   -  Reputation: 107

Like
0Likes
Like

Posted 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).


Edited by christophe-f8, 27 November 2013 - 06:24 PM.


#5 Andreas Jonsson   Moderators   -  Reputation: 3269

Like
0Likes
Like

Posted 28 November 2013 - 12:37 PM

Just to clarify the behaviour of AngelScript here.

 

When a function handle is taken for a class method, a delegate object is created to hold the handle to the class instance and the handle to the class method. This delegate object is not owned by anyone so it will be destroyed if no-one takes ownership by calling AddRef on the asIScriptFunction pointer (storing the handle in a global variable is implicitly calling AddRef).

 

When a function handle is taken for a global function, the handle points to the actual function which is owned by the module, and will thus only be released when the module is discarded. If the module had been discarded while you still held a pointer to the script function (without calling AddRef) you would have seen the same error as you did for the delegate.

 

Regards,

Andreas


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#6 christophe-f8   Members   -  Reputation: 107

Like
0Likes
Like

Posted 01 December 2013 - 08:21 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS