Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmake/libremidi.tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,11 @@ if(LIBREMIDI_HAS_PIPEWIRE)
add_test(NAME ${_pwtest}_test COMMAND ${_pwtest}_test)
endforeach()
endif()

if(LIBREMIDI_HAS_WINMM)
add_executable(winmm_output_lifetime_test tests/unit/winmm_output_lifetime.cpp)
target_include_directories(winmm_output_lifetime_test PRIVATE "${PROJECT_SOURCE_DIR}/include")
target_compile_definitions(winmm_output_lifetime_test PRIVATE LIBREMIDI_NO_BOOST UNICODE _UNICODE)
target_link_libraries(winmm_output_lifetime_test PRIVATE winmm)
add_test(NAME winmm_output_lifetime_test COMMAND winmm_output_lifetime_test)
endif()
6 changes: 4 additions & 2 deletions include/libremidi/backends/winmm/midi_out.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class midi_out_winmm final

[[nodiscard]] stdx::error do_open(unsigned int portNumber)
{
MMRESULT result = midiOutOpen(&this->outHandle, portNumber, 0, 0, CALLBACK_NULL);
HMIDIOUT handle{};
MMRESULT result = midiOutOpen(&handle, portNumber, 0, 0, CALLBACK_NULL);
if (result != MMSYSERR_NOERROR)
{
libremidi_handle_error(
Expand All @@ -45,6 +46,7 @@ class midi_out_winmm final
return from_mmerr(result);
}

outHandle = handle;
return stdx::error{};
}

Expand Down Expand Up @@ -149,7 +151,7 @@ class midi_out_winmm final
}

private:
HMIDIOUT outHandle; // Handle to Midi Output Device
HMIDIOUT outHandle{}; // Null until midiOutOpen succeeds.
std::vector<char> buffer;
};

Expand Down
64 changes: 64 additions & 0 deletions tests/unit/winmm_output_lifetime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <libremidi/config.hpp>
#include <libremidi/backends/winmm/error_domain.hpp>

#include <cstdio>

namespace
{
unsigned closes{};
bool wrong_handle{};
int device{};
HMIDIOUT handle() { return reinterpret_cast<HMIDIOUT>(&device); }
MMRESULT open_output(HMIDIOUT* out, UINT id, DWORD_PTR, DWORD_PTR, DWORD)
{
// The output parameter establishes ownership only on success.
*out = handle();
return id == 0 ? MMSYSERR_NOERROR : MMSYSERR_BADDEVICEID;
}
MMRESULT close_output(HMIDIOUT h)
{
++closes;
wrong_handle |= h != handle();
return MMSYSERR_NOERROR;
}
}

// Compile the backend in its own test executable, without opening a user's device.
#define midiOutOpen open_output
#define midiOutClose close_output
#define midiOutGetNumDevs() 0u
#include <libremidi/backends/winmm/midi_out.hpp>
#undef midiOutOpen
#undef midiOutClose
#undef midiOutGetNumDevs

int main()
{
using libremidi::midi_out_winmm;
{
midi_out_winmm unopened{{}, {}};
}
{
midi_out_winmm missing{{}, {}};
if (missing.open_port({}, {}) != std::errc::invalid_argument)
return 1;
}
{
midi_out_winmm failed{{}, {}};
if (failed.do_open(1) == stdx::error{})
return 1;
}
if (closes != 0)
{
std::fprintf(stderr, "closed %u unowned handles\n", closes);
return 1;
}
{
midi_out_winmm opened{{}, {}};
if (opened.do_open(0) != stdx::error{})
return 1;
opened.close_port();
opened.close_port();
}
return closes == 1 && !wrong_handle ? 0 : 1;
}
Loading