Skip to content

Commit 1cf1cc2

Browse files
committed
fix memory leaks
1 parent 01cb19b commit 1cf1cc2

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

midi_linux.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ func (d *Device) Open() error {
4545
id = C.CString(d.ID)
4646
result = C.Midi_open(id)
4747
)
48+
defer C.free(unsafe.Pointer(id))
49+
4850
if result.error != 0 {
4951
return errors.Errorf("error opening device %d", result.error)
5052
}
5153
d.conn = result.midi
52-
C.free(unsafe.Pointer(id))
5354
return nil
5455
}
5556

@@ -133,11 +134,11 @@ func getCardDevices(card C.int) ([]*Device, error) {
133134
ctl *C.snd_ctl_t
134135
name = C.CString(fmt.Sprintf("hw:%d", card))
135136
)
137+
defer C.free(unsafe.Pointer(name))
138+
136139
if rc := C.snd_ctl_open(&ctl, name, 0); rc != 0 {
137140
return nil, alsaMidiError(rc)
138141
}
139-
C.free(unsafe.Pointer(name))
140-
141142
var (
142143
cardDevices = []*Device{}
143144
device C.int = -1
@@ -166,6 +167,8 @@ func getDeviceDevices(ctl *C.snd_ctl_t, card C.int, device C.uint) ([]*Device, e
166167
C.snd_rawmidi_info_malloc(&info)
167168
C.snd_rawmidi_info_set_device(info, device)
168169

170+
defer C.snd_rawmidi_info_free(info)
171+
169172
// Get inputs.
170173
var subsIn C.uint
171174
C.snd_rawmidi_info_set_stream(info, C.SND_RAWMIDI_STREAM_INPUT)

0 commit comments

Comments
 (0)