| @@ -321,9 +321,25 @@ private: | |||||
| //============================================================================== | //============================================================================== | ||||
| struct WindowsOutputWrapper : public OutputWrapper | struct WindowsOutputWrapper : public OutputWrapper | ||||
| { | { | ||||
| struct MidiOutHandle | |||||
| struct MidiOutHandle : public ReferenceCountedObject | |||||
| { | { | ||||
| int refCount; | |||||
| using Ptr = ReferenceCountedObjectPtr<MidiOutHandle>; | |||||
| MidiOutHandle (WindowsMidiService& parent, UINT id, HMIDIOUT h) | |||||
| : owner (parent), deviceId (id), handle (h) | |||||
| { | |||||
| owner.activeOutputHandles.add (this); | |||||
| } | |||||
| ~MidiOutHandle() | |||||
| { | |||||
| if (handle != nullptr) | |||||
| midiOutClose (handle); | |||||
| owner.activeOutputHandles.removeFirstMatchingValue (this); | |||||
| } | |||||
| WindowsMidiService& owner; | |||||
| UINT deviceId; | UINT deviceId; | ||||
| HMIDIOUT handle; | HMIDIOUT handle; | ||||
| @@ -356,7 +372,6 @@ private: | |||||
| if (activeHandle->deviceId == deviceId) | if (activeHandle->deviceId == deviceId) | ||||
| { | { | ||||
| activeHandle->refCount++; | |||||
| han = activeHandle; | han = activeHandle; | ||||
| return; | return; | ||||
| } | } | ||||
| @@ -369,11 +384,7 @@ private: | |||||
| if (res == MMSYSERR_NOERROR) | if (res == MMSYSERR_NOERROR) | ||||
| { | { | ||||
| han = new MidiOutHandle(); | |||||
| han->deviceId = deviceId; | |||||
| han->refCount = 1; | |||||
| han->handle = h; | |||||
| parent.activeOutputHandles.add (han); | |||||
| han = new MidiOutHandle (parent, deviceId, h); | |||||
| return; | return; | ||||
| } | } | ||||
| @@ -386,15 +397,6 @@ private: | |||||
| throw std::runtime_error ("Failed to create Windows output device wrapper"); | throw std::runtime_error ("Failed to create Windows output device wrapper"); | ||||
| } | } | ||||
| ~WindowsOutputWrapper() | |||||
| { | |||||
| if (parent.activeOutputHandles.contains (han.get()) && --(han->refCount) == 0) | |||||
| { | |||||
| midiOutClose (han->handle); | |||||
| parent.activeOutputHandles.removeFirstMatchingValue (han.get()); | |||||
| } | |||||
| } | |||||
| void sendMessageNow (const MidiMessage& message) override | void sendMessageNow (const MidiMessage& message) override | ||||
| { | { | ||||
| if (message.getRawDataSize() > 3 || message.isSysEx()) | if (message.getRawDataSize() > 3 || message.isSysEx()) | ||||
| @@ -489,7 +491,7 @@ private: | |||||
| WindowsMidiService& parent; | WindowsMidiService& parent; | ||||
| String deviceName; | String deviceName; | ||||
| ScopedPointer<MidiOutHandle> han; | |||||
| MidiOutHandle::Ptr han; | |||||
| JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowsOutputWrapper) | JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowsOutputWrapper) | ||||
| }; | }; | ||||