Open MartinRichards23 opened 4 years ago
Hello MartinRichards23, thank you for opening an issue with us!
I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌
@MartinRichards23 can you please provide the steps you are taking from the beginning and what windows build number and target version being used?
Hi Kyaa-dost, we are targeting version 1903, build 18362, minimum version Fall Creators update, build 16299.
Our code is essentially the same as the demo provided here https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/PrintHelper/PrintHelperPage.xaml.cs
We are using App Center for logging, this issue seems rare so its not something I've been able to reproduce.
I'm investigating this issue to see if I can repro it in the sample app or my own projects.
Given the stack trace from @MartinRichards23, it seems like either _canvasContainer or _printCanvas is null when this code is executed in DetachCanvas():
await DispatcherQueue.EnqueueAsync(() =>
{
_canvasContainer.Children.Remove(_printCanvas);
_printCanvas.Children.Clear();
});
I could add null checks for both, but I can't repro the original issue. I hate to create a PR for something without reproducing the issue and showing that it's resolved with the change.
_printCanvas seems more likely. The parameter that sets _canvasContainer is checked for null, and the value is not changed/removed later.
@MartinRichards23 ⬆️
I can't add any more insight as to the cause of the issue, as I only have some error logs to go on, and haven't reproduced the issue myself.
Isn't it sensible to always check for null if a null is possible?
Took a quick look at this. Feel like if _printCanvas
is null then it means the PrintHelper
has already been disposed, as that's the only time it's cleared from its initialization. But then this is only called after a Print has been completed (either successfully or not), so it'd be weird for it to be cleaned-up in-between. Could this be a case of a print being initiated and then the app closed?
The _canvasContainer
is an external reference, so maybe we should have a WeakReference
or something to not hold the parent? However in any of these cases, I'm not sure what the expected result/outcome would want to be?
Would be good to have repro steps to see if we can catch this some how. Also just wondering if we want to clean-up how the object is disposed in general since we don't have unmanaged resources and check in other spots instead if its already been disposed? (https://vkontech.com/the-dispose-pattern-step-by-step/ https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose)
Describe the bug
We are occasionally getting null reference errors from users in our logging, originating from the PrintHelper.
I haven't been able to reproduce the issue, only seen in the logs, I have provided the stack trace.
Is the fix as simple as adding a null ref check in the PrintHelper class?
Environment
It doesn't seem to be specific to any device, Toolkit or Windows verison.