fix(windows): Refactor static globals to instance members#1970
Conversation
63243ea to
1c28518
Compare
…multiple windows - Replaced all `static inline` global variables with instance members in `AudioplayersWindowsPlugin`. - Ensured each Engine instance possesses its own independent `MethodChannel` and `EventStreamHandler`. - Fixed crashes occurring when opening a second window due to global static pointer overwriting. - Isolated plugin state across different windows to prevent interference.
| playerEventChannels; | ||
| // Map to keep player event handlers alive | ||
| std::map<std::string, std::unique_ptr<EventStreamHandler<EncodableValue>>> | ||
| playerEventHandlers; |
There was a problem hiding this comment.
This variable actually is never filled with handlers. But it might not be needed in the first place?
|
|
||
| // Keep the event channel and handler alive as long as the plugin/player | ||
| // exists | ||
| playerEventChannels[playerId] = std::move(eventChannel); |
There was a problem hiding this comment.
I wonder if this is actually necessary to hold the values in an extra array. Theoretically the channels should be referenced by the AudioPlayer instance and thus in the audioPlayers map / array. So they should be kept alive as long as the players are present in the array, or am I mistaken?
| auto handler = std::make_unique<EventStreamHandler<EncodableValue>>(); | ||
| plugin->globalEvents = handler.get(); | ||
|
|
||
| plugin->globalEventChannel->SetStreamHandler(std::move(handler)); |
There was a problem hiding this comment.
Just a question: do you think it would make sense to have a constructor taking all the initial parameters, similar to Flutter plugins:
https://github.com/flutter/packages/blob/a0ad9d99b16e1879f597f9c0b0150cb01f29a57e/packages/camera/camera_windows/windows/camera_plugin.cpp#L122
|
Sorry for the big delay. Thank you for the contribution! A few questions / proposals still, if you have a moment :) |
fix(windows): support multi-engine and fix crashes in multi-window environments
Description
Previously,
AudioplayersWindowsPluginutilizedstatic inlineglobal variables for theMethodChannelandEventStreamHandler. In multi-window scenarios (e.g., usingdesktop_multi_window), where multiple Flutter Engines are initialized, these static pointers were overwritten by the most recently opened window. This caused the plugin state of previous windows to become corrupted, leading to crashes and inconsistent behavior.This PR refactors the plugin to move these globals into instance members. This ensures:
MethodChannelandEventStreamHandler.Checklist
fix:,feat:,refactor:,docs:,chore:,test:,ci:etc).///, where necessary.Breaking Change
Related Issues
Resolves crashes in multi-window/multi-engine environments on Windows.