3

Closed

[Phone] NullReferenceException in TaskController

description

Hi,

I'm getting null ref errors in TaskController.OnTaskComplete method because the "request" field is null. I dug deeper and while I don't have a solid understanding of why it's happening here is what is happening:
  1. Page A does EventAggregator.RequestTask<PhotoChooserTask>()
  2. The PhotoChooserTask completes, Page A's async void Handle(TaskCompleted<PhotoResult> completed) fires.
  3. Page A navigates to Page B which does EventAggregator.RequestTask<ShareMediaTask>()
  4. Pres Back button twice to get back to Page A.
  5. Page A does EventAggregator.RequestTask<PhotoChooserTask>()
  6. The PhotoChooserTask completes and the NullReferenceException crash happens in the TaskController.OnTaskComplete.
The culprit is this, on step 5 the TaskController handles the TaskExecutionRequested and executes this bit of code:
            var @event = taskType.GetEvent("Completed");
            if(@event != null) {
                request = message;
                @event.AddEventHandler(request.Task, CreateOnTaskCompletedDelegate(@event));
            }
As soon as the event handler is attached the Chooser's event fires with TaskResult.Cancel result. TaskController.OnTaskComplete executes and clears out the request field.

So when the Chooser is actually shown and completes and OnTaskComplete is fired again the request field is null.
Closed Feb 20 at 5:02 PM by EisenbergEffect
Moved to github

comments

vishnevetskaya wrote Sep 26, 2013 at 2:12 PM

tibel wrote Sep 26, 2013 at 4:08 PM

Try change your code to use IHandleWithTask instead of IHandle so you don't have an async void:
public async Task Handle(TaskCompleted<PhotoResult> completed) {
}
It is most likely a problem with "async void".

vishnevetskaya wrote Sep 26, 2013 at 5:01 PM

Nope, that didn't fix it.
System.NullReferenceException: Object reference not set to an instance of an object.
at Caliburn.Micro.TaskController.OnTaskComplete(Object sender, EventArgs e)
at Microsoft.Phone.Tasks.ChooserBase`1.FireCompleted(Object sender, TTaskEventArgs e, Delegate fireThisHandlerOnly)
at Microsoft.Phone.Tasks.PhotoChooserTask.OnInvokeReturned(Byte[] outputBuffer, Delegate fireThisHandlerOnly)
at Microsoft.Phone.Tasks.GenericChooser.GenericOnInvokeReturned.Invoke(Byte[] outputBuffer, Delegate d)
at Microsoft.Phone.Tasks.GenericChooser.OnInvokeReturned(Byte[] outputBuffer, Delegate d)
at Microsoft.Phone.Tasks.ChooserListener.OnChildTaskReturned(ChildTaskReturnedEventArgs args)
at Microsoft.Phone.TaskModel.Interop.Task.FireOnChildTaskReturned(IntPtr returnDataPtr, UInt32 returnDataSize)

tibel wrote Sep 28, 2013 at 10:06 AM

May you attach a small sample application that shows the issue, please.
I need to check for side effects before integrating your fix.

vishnevetskaya wrote Sep 28, 2013 at 5:16 PM

Not in near future as it's crunch time here. May be in a couple weeks.

tibel wrote Oct 20, 2013 at 7:43 AM

Possible workaround:
https://caliburnmicro.codeplex.com/SourceControl/network/forks/vishnevetskaya/caliburnmicro/changeset/592b86ddff526f837c3ad349cb80977685871a94

It would be of help to have a sample application for this issue.

Only guessing here:
It might have to do with executing a task inside a task-completed handler. Then it would be a problem with the right sequence in TaskController to set request.