Skip to content

Commit

Permalink
C api: track CallbackProxyUserData and delete it
Browse files Browse the repository at this point in the history
Fixes thestk#104.
  • Loading branch information
radarsat1 committed Mar 2, 2017
1 parent cf0035f commit 7338b8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
39 changes: 23 additions & 16 deletions rtmidi_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
#include "rtmidi_c.h"
#include "RtMidi.h"

class CallbackProxyUserData
{
public:
CallbackProxyUserData (RtMidiCCallback cCallback, void *userData)
: c_callback (cCallback), user_data (userData)
{
}
RtMidiCCallback c_callback;
void *user_data;
};

/* misc */
int rtmidi_sizeof_rtmidi_api ()
{
Expand Down Expand Up @@ -133,11 +144,13 @@ RtMidiInPtr rtmidi_in_create (enum RtMidiApi api, const char *clientName, unsign
RtMidiIn* rIn = new RtMidiIn ((RtMidi::Api) api, name, queueSizeLimit);

wrp->ptr = (void*) rIn;
wrp->data = 0;
wrp->ok = true;
wrp->msg = "";

} catch (const RtMidiError & err) {
wrp->ptr = 0;
wrp->ptr = 0;
wrp->data = 0;
wrp->ok = false;
wrp->msg = err.what ();
}
Expand All @@ -147,6 +160,8 @@ RtMidiInPtr rtmidi_in_create (enum RtMidiApi api, const char *clientName, unsign

void rtmidi_in_free (RtMidiInPtr device)
{
if (device->data)
delete (CallbackProxyUserData*) device->data;
delete (RtMidiIn*) device->ptr;
delete device;
}
Expand All @@ -164,17 +179,7 @@ enum RtMidiApi rtmidi_in_get_current_api (RtMidiPtr device)
}
}

class CallbackProxyUserData
{
public:
CallbackProxyUserData (RtMidiCCallback cCallback, void *userData)
: c_callback (cCallback), user_data (userData)
{
}
RtMidiCCallback c_callback;
void *user_data;
};

static
void callback_proxy (double timeStamp, std::vector<unsigned char> *message, void *userData)
{
CallbackProxyUserData* data = reinterpret_cast<CallbackProxyUserData*> (userData);
Expand All @@ -183,21 +188,23 @@ void callback_proxy (double timeStamp, std::vector<unsigned char> *message, void

void rtmidi_in_set_callback (RtMidiInPtr device, RtMidiCCallback callback, void *userData)
{
device->data = (void*) new CallbackProxyUserData (callback, userData);
try {
void *data = (void *) new CallbackProxyUserData (callback, userData);
((RtMidiIn*) device->ptr)->setCallback (callback_proxy, data);

((RtMidiIn*) device->ptr)->setCallback (callback_proxy, device->data);
} catch (const RtMidiError & err) {
device->ok = false;
device->msg = err.what ();
delete (CallbackProxyUserData*) device->data;
device->data = 0;
}
}

void rtmidi_in_cancel_callback (RtMidiInPtr device)
{
try {
((RtMidiIn*) device->ptr)->cancelCallback ();

delete (CallbackProxyUserData*) device->data;
device->data = 0;
} catch (const RtMidiError & err) {
device->ok = false;
device->msg = err.what ();
Expand Down
1 change: 1 addition & 0 deletions rtmidi_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern "C" {

struct RtMidiWrapper {
void* ptr;
void* data;
bool ok;
const char* msg;
};
Expand Down

0 comments on commit 7338b8f

Please sign in to comment.