Crash of the .exe using Listeners

Hello everyone,

I am having troubles using listeners, but only in the program executable of my game. Everything works fine in the game window of my Editor.

Let me explain :

I create input fields dynamically for each item of a list. I add a new listener on the onEndEdit event for each input field :

public void AddInputField( int index )
{
    GameObject newInputFielGameObject = GameObject.Instantiate( InputFieldPrefab.gameObject ) ;
    InputField inputField = newInputFielGameObject .GetComponentInChildren<InputField>() ;

    // [ ... Setting the item position, name, parent, eventually focus, ... ]

    // Add a listener to check the value of the input when the input field is not edited anymore 
    inputField.onEndEdit.AddListener( s => CheckValue( s, newInputFielGameObject  ) ) ;			
}

And here is the CheckValueFunction (lightened) :

public void CheckValue( string inputValue, GameObject inputFieldGameObject )
{
    if( string.IsNullOrEmpty( inputValue.Trim() ) )
    {
        Destroy( inputFieldGameObject ) ;

        UpdateList() ;
    }
    else if( !IsValid( inputValue ) ) // Custom IsValid, even without it, same problem
    {
        inputFieldGameObject.GetComponentInChildren<Image>().color = new Color( 255, 0, 0, 50);
    }
    else
    {
        inputFieldGameObject.GetComponentInChildren<Image>().color = Color.white ;

        UpdateList() ;
    }
}

So, I tried different things :

  • Use lambda expression : Same problem
  • Add a debug Log inside the CheckValue function : log in output file only, same problem
  • Add a debug Log inside the delegate, just before the call to CheckValue : log in output file only, same problem
  • Change the gameobject to its transform : same problem
  • Put the code of the CheckValue function directly inside the anonymous delegate : same problem
  • Setting the gameobject parameter as a reference (ref keyword) : same problem
  • Removing the call of CheckValue function : NO ERROR
  • Remove the gameobject (and commenting related stuff) : NO ERROR !

But unfortunately for me, I really need the gameobject as you can see !


That’s strange this crash occurs only in the exe, not in the editor.

I took a look at the output_log file, but, the exe completly crash, thus, nothing is written in the file about the problem.

EDIT : I don’t think the problem is due to the phenomenon of closure since I declare a new GameObject in the AddInputField function.


UPDATE :

In fact, even if there is no message in the exe (with the debug build), the output_log.txt tells me things :

Supposing I add some Debug messages in the AddListener function like this

inputField.onEndEdit.AddListener( s => { Debug.LogError("Entering CheckValue ...") ; CheckValue( s, newInputFielGameObject ) ; Debug.LogError("CheckValue left") ; } ) ;	

The output_log text file DOES contain the error messages … But, I don’t know where the process goes after the callback functions have been called.

I dont see any problem… maybe there is problem in your editor.

Well …

Further to some tests, the problem didn’t come from the listeners but from my UpdateList() function ! In fact, inside this function, I call the Destroy() function (to flush the list and reconstruct it) and this is this function precisely which is the source of my troubles.

I suspect the listeners in the onEndEdit are not all called before I destroy my object. Thus, before destroying the objects, I call the GetComponent().onEndEdit.RemoveAllListeners() function, and the GetComponent().delegates.Clear() function as well.

Thread closed ! :wink: