Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash single-instanced app when drag and drop #9061

Closed
Bondarenko1990 opened this issue Nov 3, 2023 · 17 comments
Closed

Crash single-instanced app when drag and drop #9061

Bondarenko1990 opened this issue Nov 3, 2023 · 17 comments
Labels
area-DragAndDrop bug Something isn't working team-Markup Issue for the Markup team

Comments

@Bondarenko1990
Copy link

Bondarenko1990 commented Nov 3, 2023

Discussed in microsoft/WindowsAppSDK#3936

Originally posted by Bondarenko1990 November 3, 2023
I remade the application according to this example Making the app single-instanced (Part 3)
Windows App SDK 1.4.231008000
And when I do a drag the grid control I get a crash:
image
Event Viewer logs:
image

@bpulliam bpulliam transferred this issue from microsoft/WindowsAppSDK Nov 9, 2023
@bpulliam bpulliam added team-Markup Issue for the Markup team area-DragAndDrop labels Nov 9, 2023
@JesseCol
Copy link

JesseCol commented Nov 9, 2023

Hi, can you help me understand what you're dragging? I built the DrumPad repo and ran the sample, I don't see any UI content that's draggable. I tried clicking and dragging in the app as well, but it has no effect.

@Bondarenko1990
Copy link
Author

Bondarenko1990 commented Nov 10, 2023

@JesseCol Yes, there are no UI content that's draggable. I have created DrumPad sample with textblock "Kick 1" that's draggable DrumpPad_link
crash_drag

@duncanmacmichael duncanmacmichael added the bug Something isn't working label Nov 11, 2023
@Juansero29
Copy link

Any news on this issue? My company is facing the same problem, we had to disable a drag and drop feature in our app after making it single instanced. Any plan on when will this be fixed?

@Juansero29
Copy link

The provided sample effectively reprodeces this crash, is there anything more we can do to help fix this problem?
devenv_pXAOwO95G8

@Scottj1s
Copy link
Member

Scottj1s commented Feb 3, 2024

@Juansero29 Yes, we're able to repro the crash internally and are investigating a fix.

@Scottj1s
Copy link
Member

Scottj1s commented Feb 3, 2024

The issue appears to be making Main async to support single-instance. Can you make it synchronous instead?

        static void Main(string[] args)
        {
            WinRT.ComWrappersSupport.InitializeComWrappers();
            Task<bool> isRedirect = DecideRedirection();
            if (!isRedirect.Result)
            ...

@mareksm
Copy link

mareksm commented Feb 3, 2024

It still crashes after making it synchronous as suggested above.

Microsoft.UI.Xaml.dll!DirectUI::RaiseDragDropEventAsyncOperation::OnStart() Line 40
	at C:\__w\1\s\dxaml\xcp\dxaml\lib\RaiseDragDropEventAsyncOperation.cpp(40)
Microsoft.UI.Xaml.dll!Microsoft::WRL::AsyncBase<ABI::Windows::Foundation::IAsyncOperationCompletedHandler<enum ABI::Windows::ApplicationModel::DataTransfer::DataPackageOperation>,Microsoft::WRL::Details::Nil,1,Microsoft::WRL::AsyncOptions<2,0,&GUID_NULL,0>>::Start() Line 632
	at C:\__w\1\s\packages\Microsoft.Windows.SDK.cpp.10.0.18362.5\c\Include\10.0.18362.0\winrt\wrl\async.h(632)
[Inline Frame] Microsoft.UI.Xaml.dll!DirectUI::DXamlAsyncBaseImpl<ABI::Windows::Foundation::IAsyncOperationCompletedHandler<enum ABI::Windows::ApplicationModel::DataTransfer::DataPackageOperation>,ABI::Windows::Foundation::IAsyncOperation<enum ABI::Windows::ApplicationModel::DataTransfer::DataPackageOperation>,Microsoft::WRL::AsyncOptions<2,0,&GUID_NULL,0>,DirectUI::IDragOperationDeferralTarget,1>::StartOperation() Line 51
	at C:\__w\1\s\dxaml\xcp\dxaml\lib\DXamlAsyncBase.h(51)
Microsoft.UI.Xaml.dll!DirectUI::DropOperationTarget::RaiseDragDropEventOperationAsync(DirectUI::DragDropMessageType type, ABI::Microsoft::UI::Input::DragDrop::IDragInfo * pDragInfo, ABI::Microsoft::UI::Input::DragDrop::IDragUIOverride * pDragUIOverride, ABI::Windows::Foundation::IAsyncOperation<enum ABI::Windows::ApplicationModel::DataTransfer::DataPackageOperation> * * ppReturnValue) Line 156
	at C:\__w\1\s\dxaml\xcp\dxaml\lib\DropOperationTarget.cpp(156)
Microsoft.UI.Xaml.dll!DirectUI::DropOperationTarget::EnterAsync(ABI::Microsoft::UI::Input::DragDrop::IDragInfo * pDragInfo, ABI::Microsoft::UI::Input::DragDrop::IDragUIOverride * pDragUIOverride, ABI::Windows::Foundation::IAsyncOperation<enum ABI::Windows::ApplicationModel::DataTransfer::DataPackageOperation> * * ppReturnValue) Line 60
	at C:\__w\1\s\dxaml\xcp\dxaml\lib\DropOperationTarget.cpp(60)
Microsoft.UI.Input.dll!winrt::impl::consume_Microsoft_UI_Input_DragDrop_IDropOperationTarget<struct winrt::Microsoft::UI::Input::DragDrop::IDropOperationTarget>::EnterAsync(struct winrt::Microsoft::UI::Input::DragDrop::DragInfo const &,struct winrt::Microsoft::UI::Input::DragDrop::DragUIOverride const &)
Microsoft.UI.Input.dll!winrt::impl::produce<struct winrt::Microsoft::UI::Input::DragDrop::implementation::SystemDropOperationTargetAdapter,struct winrt::Windows::ApplicationModel::DataTransfer::DragDrop::Core::ICoreDropOperationTarget>::EnterAsync(void *,void *,void * *)
DataExchange.dll!DropTargetInternal::DragEnter(struct IDragDropArgsPriv *,struct IDragUIOverridePriv * *)
rpcrt4.dll!Invoke�()
rpcrt4.dll!Ndr64StubWorker()
rpcrt4.dll!NdrStubCall3()
combase.dll!CStdStubBuffer_Invoke(IRpcStubBuffer * This, tagRPCOLEMESSAGE * prpcmsg, IRpcChannelBuffer * pRpcChannelBuffer) Line 1479
	at onecore\com\combase\ndr\ndrole\stub.cxx(1479)
[Inline Frame] combase.dll!InvokeStubWithExceptionPolicyAndTracing::__l6::<lambda_c9f3956a20c9da92a64affc24fdd69ec>::operator()() Line 1153
	at onecore\com\combase\dcomrem\channelb.cxx(1153)
combase.dll!ObjectMethodExceptionHandlingAction<<lambda_c9f3956a20c9da92a64affc24fdd69ec>>(InvokeStubWithExceptionPolicyAndTracing::__l6::<lambda_c9f3956a20c9da92a64affc24fdd69ec> action, ObjectMethodExceptionHandlingInfo * pExceptionHandlingInfo, ExceptionHandlingResult * pExceptionHandlingResult, void *) Line 94
	at onecore\com\combase\dcomrem\excepn.hxx(94)
[Inline Frame] combase.dll!InvokeStubWithExceptionPolicyAndTracing(IRpcStubBuffer * pMsg, tagRPCOLEMESSAGE *) Line 1151
	at onecore\com\combase\dcomrem\channelb.cxx(1151)
combase.dll!DefaultStubInvoke(bool bIsAsyncBeginMethod, IServerCall * pServerCall, IRpcChannelBuffer * pChannel, IRpcStubBuffer * pStub, unsigned long * pdwFault) Line 1220
	at onecore\com\combase\dcomrem\channelb.cxx(1220)
combase.dll!SyncServerCall::StubInvoke(IRpcChannelBuffer * pChannel, IRpcStubBuffer * pStub, unsigned long * pdwFault) Line 791
	at onecore\com\combase\dcomrem\ServerCall.hpp(791)
[Inline Frame] combase.dll!StubInvoke(tagRPCOLEMESSAGE * pMsg, const _GUID &) Line 1485
	at onecore\com\combase\dcomrem\channelb.cxx(1485)
combase.dll!ServerCall::ContextInvoke(tagIPIDEntry * ipidEntry) Line 1421
	at onecore\com\combase\dcomrem\ctxchnl.cxx(1421)
combase.dll!ComInvokeWithLockAndIPID(ServerCall * pServerCall, tagIPIDEntry * pIPIDEntry) Line 2154
	at onecore\com\combase\dcomrem\channelb.cxx(2154)
[Inline Frame] combase.dll!ThreadInvokeReturnHresult(_RPC_MESSAGE *) Line 6984
	at onecore\com\combase\dcomrem\channelb.cxx(6984)
combase.dll!ThreadInvoke(_RPC_MESSAGE * message) Line 7084
	at onecore\com\combase\dcomrem\channelb.cxx(7084)
rpcrt4.dll!DispatchToStubInCNoAvrf�()
rpcrt4.dll!RPC_INTERFACE::DispatchToStubWorker()
rpcrt4.dll!RPC_INTERFACE::DispatchToStubWithObject()
rpcrt4.dll!LRPC_SCALL::DispatchRequest()
rpcrt4.dll!LRPC_SCALL::HandleRequest(struct _PORT_MESSAGE *,struct _PORT_MESSAGE *,void *,unsigned __int64,class RPCP_ALPC_HANDLE_ATTR *)
rpcrt4.dll!LRPC_SASSOCIATION::HandleRequest()
rpcrt4.dll!LRPC_ADDRESS::HandleRequest()
rpcrt4.dll!LRPC_ADDRESS::ProcessIO(void *)
rpcrt4.dll!LrpcIoComplete()
ntdll.dll!TppAlpcpExecuteCallback()
ntdll.dll!TppWorkerThread�()
kernel32.dll!BaseThreadInitThunk()
ntdll.dll!RtlUserThreadStart�()

@mareksm
Copy link

mareksm commented Feb 3, 2024

All three d&d event handlers are empty in my case:

    void DropHandler(IInspectable const& target, DragEventArgs const& args)
    {
        log_debug("DropHandler");
    }

    void DragEnterHandler(IInspectable const& target, DragEventArgs const& args)
    {
        log_debug("DragEnterHandler");
    }

    void DragOverHandler(IInspectable const& target, DragEventArgs const& args)
    {
        log_debug("DragOverHandler");
    }

@mareksm
Copy link

mareksm commented Feb 3, 2024

And being registered on ListView as follows:

        testview = ListView();
        testview.AllowDrop(true);
        testview.Drop({this, &MainWindow::DropHandler});
        testview.DragEnter({this, &MainWindow::DragEnterHandler});
        testview.DragOver({this, &MainWindow::DragOverHandler});

@Scottj1s
Copy link
Member

Scottj1s commented Feb 3, 2024

Thanks @mareksm - is yours the DrumPad repro attached earlier to this issue? If it's different, could you attach your repro and/or a crash dump here?

@kmgallahan
Copy link
Contributor

kmgallahan commented Feb 3, 2024

@Scottj1s @Bondarenko1990 I did this and drag n drop works without crashing (although an error shows for Main that doesn't prevent building:

<!-- added to .csproj to get rid of an error message -->
<StartupObject>DrumPad.Program</StartupObject> 
[STAThread]
static int Main(string[] args)
{
    WinRT.ComWrappersSupport.InitializeComWrappers();
    bool isRedirect = DecideRedirection();
    if (!isRedirect)
    {
        Microsoft.UI.Xaml.Application.Start((p) =>
        {
            var context = new DispatcherQueueSynchronizationContext(
                DispatcherQueue.GetForCurrentThread());
            SynchronizationContext.SetSynchronizationContext(context);
            new App();
        });
    }
    return 0;
}

private static bool DecideRedirection()
{
    bool isRedirect = false;
    AppActivationArguments args = AppInstance.GetCurrent().GetActivatedEventArgs();
    ExtendedActivationKind kind = args.Kind;
    AppInstance keyInstance = AppInstance.FindOrRegisterForKey("randomKey");

    if (keyInstance.IsCurrent)
    {
        keyInstance.Activated += OnActivated;
    }
    else
    {
        isRedirect = true;
        keyInstance.RedirectActivationToAsync(args).GetAwaiter().GetResult();
    }
    return isRedirect;
}

Note that as with all drag n drop operations, this spams non-fatal native exceptions at a very high rate as reported here #8851:

onecoreuap\shell\dataexchange\winrt\lib\droptargetinternal.cpp(306)\DataExchange.dll!00007FF908DF6988: (caller: 00007FF930C3B583) ReturnHr(1) tid(8cdc) 80070490 Element not found.
onecoreuap\shell\dataexchange\winrt\lib\droptargetinternal.cpp(306)\DataExchange.dll!00007FF908DF6988: (caller: 00007FF930C3B583) ReturnHr(2) tid(8cdc) 80070490 Element not found.
onecoreuap\shell\dataexchange\winrt\lib\droptargetinternal.cpp(306)\DataExchange.dll!00007FF908DF6988: (caller: 00007FF930C3B583) ReturnHr(3) tid(8cdc) 80070490 Element not found.
onecoreuap\shell\dataexchange\winrt\lib\droptargetinternal.cpp(306)\DataExchange.dll!00007FF908DF6988: (caller: 00007FF930C3B583) ReturnHr(4) tid(8cdc) 80070490 Element not found.
onecoreuap\shell\dataexchange\winrt\lib\droptargetinternal.cpp(306)\DataExchange.dll!00007FF908DF6988: (caller: 00007FF930C3B583) ReturnHr(5) tid(8cdc) 80070490 Element not found.
onecoreuap\shell\dataexchange\winrt\lib\droptargetinternal.cpp(306)\DataExchange.dll!00007FF908DF6988: (caller: 00007FF930C3B583) ReturnHr(6) tid(8cdc) 80070490 Element not found.
onecoreuap\shell\dataexchange\winrt\lib\droptargetinternal.cpp(306)\DataExchange.dll!00007FF908DF6988: (caller: 00007FF930C3B583) ReturnHr(7) tid(8cdc) 80070490 Element not found.
onecoreuap\shell\dataexchange\winrt\lib\droptargetinternal.cpp(306)\DataExchange.dll!00007FF908DF6988: (caller: 00007FF930C3B583) ReturnHr(8) tid(8cdc) 80070490 Element not found.
onecoreuap\shell\dataexchange\winrt\lib\droptargetinternal.cpp(306)\DataExchange.dll!00007FF908DF6988: (caller: 00007FF930C3B583) ReturnHr(9) tid(8cdc) 80070490 Element not found.

@Scottj1s
Copy link
Member

Scottj1s commented Feb 5, 2024

@kmgallahan yes, your solution of making Main sync is what I found to work as well. What error are you seeing for Main? Btw, you can make it void since it's no longer returning a Task.

@kmgallahan
Copy link
Contributor

@Scottj1s VS reported an error about multiple entry points being defined with a red squigle under Main. Everything built and ran fine though, and the error goes away by adding the line from my last comment to the csproj.

@Scottj1s
Copy link
Member

Scottj1s commented Feb 5, 2024

@kmgallahan I suspect you need to add this to your csproj:
<DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>

@kmgallahan
Copy link
Contributor

@Scottj1s I was just running the repo the OP provided which includes this line:

	<defineconstants>DISABLE_XAML_GENERATED_MAIN</defineconstants>

Changing capitalization and cleaning the project didn't matter:

Screenshot 2024-02-05 153406

App.g.i.cs shows that a main still gets generated, so I guess VS can't work out what is going on:

namespace DrumPad
{
#if !DISABLE_XAML_GENERATED_MAIN
    /// <summary>
    /// Program class
    /// </summary>
    public static class Program
    {
        [global::System.Runtime.InteropServices.DllImport("Microsoft.ui.xaml.dll")]
        private static extern void XamlCheckProcessRequirements();

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.UI.Xaml.Markup.Compiler"," 3.0.0.2306")]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.STAThreadAttribute]
        static void Main(string[] args)
        {
            XamlCheckProcessRequirements();
            
            global::WinRT.ComWrappersSupport.InitializeComWrappers();
            global::Microsoft.UI.Xaml.Application.Start((p) => {
                var context = new global::Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext(global::Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread());
                global::System.Threading.SynchronizationContext.SetSynchronizationContext(context);
                new App();
            });
        }
    }
#endif

@mareksm
Copy link

mareksm commented Feb 6, 2024

In my case (C++ app) adding this to WinMain:

winrt::init_apartment(winrt::apartment_type::single_threaded);

fixed D&D crash issue. Everything else, including single-instance code in OnLaunched remain unchanged.

@Scottj1s
Copy link
Member

Scottj1s commented Feb 6, 2024

Closing this issue as it appears that everyone posting here has found their solution/workaround.

@Scottj1s Scottj1s closed this as completed Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DragAndDrop bug Something isn't working team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

8 participants