Skip to content

Commit

Permalink
fix free pointer on Win. Test ok
Browse files Browse the repository at this point in the history
  • Loading branch information
alnitak committed Jun 3, 2024
1 parent 60f6a26 commit 7738a36
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
},
{
Expand All @@ -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}"
},
{
Expand Down
6 changes: 3 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
7 changes: 6 additions & 1 deletion example/windows/flutter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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 $<CONFIG>
${FLUTTER_TARGET_PLATFORM} $<CONFIG>
VERBATIM
)
add_custom_target(flutter_assemble DEPENDS
Expand Down
28 changes: 22 additions & 6 deletions lib/src/bindings_player_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ class FlutterSoLoudFfi {
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void nativeFree(ffi.Pointer<ffi.Void> pointer) {
return _nativeFree(pointer);
}

late final _nativeFreePtr =
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void>)>>(
'nativeFree');
late final _nativeFree =
_nativeFreePtr.asFunction<void Function(ffi.Pointer<ffi.Void>)>();

/// Controller to listen to voice ended events.
late final StreamController<int> voiceEndedEventController =
StreamController.broadcast();
Expand All @@ -91,7 +101,10 @@ class FlutterSoLoudFfi {
void _voiceEndedCallback(ffi.Pointer<ffi.UnsignedInt> 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<ffi.Void>());

}

/// Controller to listen to file loaded events.
Expand All @@ -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<ffi.Void>());
nativeFree(completeFileName.cast<ffi.Void>());
nativeFree(hash.cast<ffi.Void>());
}

/// Controller to listen to voice ended events.
Expand All @@ -133,9 +147,11 @@ class FlutterSoLoudFfi {

void _stateChangedCallback(ffi.Pointer<ffi.Int32> 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<ffi.Void>());
_log.fine(() => 'STATE CHANGED EVENT state: $s');
stateChangedController.add(s);
calloc.free(state);
}

/// Set a Dart function to call when a sound ends.
Expand Down
4 changes: 4 additions & 0 deletions src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/ffi_gen_tmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 7738a36

Please sign in to comment.