diff --git a/.vscode/launch.json b/.vscode/launch.json index ff6e613..8e76531 100755 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -42,7 +42,7 @@ "type": "cppvsdbg", "request": "launch", "args": [], - "program": "${workspaceFolder}/example/build/windows/runner/Debug/flutter_soloud_example.exe", + "program": "${workspaceFolder}/example/build/windows/x64/runner/Debug/flutter_soloud_example.exe", "cwd": "${workspaceFolder}" }, { @@ -51,7 +51,7 @@ "type": "cppvsdbg", "request": "launch", "args": [], - "program": "${workspaceFolder}/example/build/windows/runner/Debug/flutter_soloud_example.exe", + "program": "${workspaceFolder}/example/build/windows/x64//runner/Debug/flutter_soloud_example.exe", "cwd": "${workspaceFolder}" }, { diff --git a/example/pubspec.lock b/example/pubspec.lock index 8aa2b73..5ca483a 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -392,10 +392,10 @@ packages: dependency: transitive description: name: win32 - sha256: a79dbe579cb51ecd6d30b17e0cae4e0ea15e2c0e66f69ad4198f22a6789e94f4 + sha256: "0eaf06e3446824099858367950a813472af675116bf63f008a4c2a75ae13e9cb" url: "https://pub.dev" source: hosted - version: "5.5.1" + version: "5.5.0" xdg_directories: dependency: transitive description: @@ -405,5 +405,5 @@ packages: source: hosted version: "1.0.4" sdks: - dart: ">=3.4.0 <4.0.0" + dart: ">=3.3.0 <4.0.0" flutter: ">=3.18.0-18.0.pre.54" diff --git a/example/windows/flutter/CMakeLists.txt b/example/windows/flutter/CMakeLists.txt index 930d207..903f489 100755 --- a/example/windows/flutter/CMakeLists.txt +++ b/example/windows/flutter/CMakeLists.txt @@ -10,6 +10,11 @@ include(${EPHEMERAL_DIR}/generated_config.cmake) # https://github.com/flutter/flutter/issues/57146. set(WRAPPER_ROOT "${EPHEMERAL_DIR}/cpp_client_wrapper") +# Set fallback configurations for older versions of the flutter tool. +if (NOT DEFINED FLUTTER_TARGET_PLATFORM) + set(FLUTTER_TARGET_PLATFORM "windows-x64") +endif() + # === Flutter Library === set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/flutter_windows.dll") @@ -92,7 +97,7 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E env ${FLUTTER_TOOL_ENVIRONMENT} "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat" - windows-x64 $ + ${FLUTTER_TARGET_PLATFORM} $ VERBATIM ) add_custom_target(flutter_assemble DEPENDS diff --git a/lib/src/bindings_player_ffi.dart b/lib/src/bindings_player_ffi.dart index 74cb20b..afdf1f2 100644 --- a/lib/src/bindings_player_ffi.dart +++ b/lib/src/bindings_player_ffi.dart @@ -80,6 +80,16 @@ class FlutterSoLoudFfi { /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// + void nativeFree(ffi.Pointer pointer) { + return _nativeFree(pointer); + } + + late final _nativeFreePtr = + _lookup)>>( + 'nativeFree'); + late final _nativeFree = + _nativeFreePtr.asFunction)>(); + /// Controller to listen to voice ended events. late final StreamController voiceEndedEventController = StreamController.broadcast(); @@ -91,7 +101,10 @@ class FlutterSoLoudFfi { void _voiceEndedCallback(ffi.Pointer handle) { _log.fine(() => 'VOICE ENDED EVENT handle: ${handle.value}'); voiceEndedEventController.add(handle.value); - calloc.free(handle); + // Must free a pointer made on cpp. On Windows this must be freed + // there and cannot use `calloc.free(...)` + nativeFree(handle.cast()); + } /// Controller to listen to file loaded events. @@ -117,10 +130,11 @@ class FlutterSoLoudFfi { 'hash': hash.value, }; fileLoadedEventsController.add(result); - calloc - ..free(error) - ..free(completeFileName) - ..free(hash); + // Must free a pointer made on cpp. On Windows this must be freed + // there and cannot use `calloc.free(...)` + nativeFree(error.cast()); + nativeFree(completeFileName.cast()); + nativeFree(hash.cast()); } /// Controller to listen to voice ended events. @@ -133,9 +147,11 @@ class FlutterSoLoudFfi { void _stateChangedCallback(ffi.Pointer state) { final s = PlayerStateNotification.values[state.value]; + // Must free a pointer made on cpp. On Windows this must be freed + // there and cannot use `calloc.free(state)` + nativeFree(state.cast()); _log.fine(() => 'STATE CHANGED EVENT state: $s'); stateChangedController.add(s); - calloc.free(state); } /// Set a Dart function to call when a sound ends. diff --git a/src/bindings.cpp b/src/bindings.cpp index 68ef5b8..e2ea962 100644 --- a/src/bindings.cpp +++ b/src/bindings.cpp @@ -31,6 +31,10 @@ extern "C" void (*dartFileLoadedCallback)(enum PlayerErrors *, char *completeFileName, unsigned int *) = nullptr; void (*dartStateChangedCallback)(enum PlayerStateEvents *) = nullptr; + FFI_PLUGIN_EXPORT void nativeFree(void *pointer) { + free(pointer); + } + /// The callback to monitor when a voice ends. /// /// It is called by void `Soloud::stopVoice_internal(unsigned int aVoice)` when a voice ends. diff --git a/src/ffi_gen_tmp.h b/src/ffi_gen_tmp.h index a7fa593..ae0a34f 100644 --- a/src/ffi_gen_tmp.h +++ b/src/ffi_gen_tmp.h @@ -34,3 +34,5 @@ FFI_PLUGIN_EXPORT void setDartEventCallback( dartVoiceEndedCallback_t voice_ended_callback, dartFileLoadedCallback_t file_loaded_callback, dartStateChangedCallback_t state_changed_callback); + +FFI_PLUGIN_EXPORT void nativeFree(void *pointer); \ No newline at end of file