Skip to content

Commit

Permalink
MidiManagerAlsa: memory leak fix of snd_midi_event_t
Browse files Browse the repository at this point in the history
There was an error path that caused memory leaks of snd_midi_event_t.
This patch enforces all code path to use ScopedSndMidiEventPtr.

Bug: 759756
Change-Id: Icc2f85fccc66ad9714af18ed5bd90c7f009f4dbb
Reviewed-on: https://chromium-review.googlesource.com/643030
Reviewed-by: Adam Goode <[email protected]>
Commit-Queue: Takashi Toyoshima <[email protected]>
Cr-Commit-Position: refs/heads/master@{#499133}
  • Loading branch information
toyoshim authored and Commit Bot committed Sep 1, 2017
1 parent 18f895e commit 0c0059c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 10 additions & 8 deletions media/midi/midi_manager_alsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,7 @@ void MidiManagerAlsa::StartInitialization() {
}

// Initialize decoder.
snd_midi_event_t* tmp_decoder = nullptr;
snd_midi_event_new(0, &tmp_decoder);
ScopedSndMidiEventPtr decoder(tmp_decoder);
tmp_decoder = nullptr;
ScopedSndMidiEventPtr decoder = CreateScopedSndMidiEventPtr(0);
snd_midi_event_no_status(decoder.get(), 1);

// Initialize udev and monitor.
Expand Down Expand Up @@ -856,11 +853,10 @@ std::string MidiManagerAlsa::AlsaCard::ExtractManufacturerString(
void MidiManagerAlsa::SendMidiData(MidiManagerClient* client,
uint32_t port_index,
const std::vector<uint8_t>& data) {
snd_midi_event_t* encoder;
snd_midi_event_new(kSendBufferSize, &encoder);
ScopedSndMidiEventPtr encoder = CreateScopedSndMidiEventPtr(kSendBufferSize);
for (const auto datum : data) {
snd_seq_event_t event;
int result = snd_midi_event_encode_byte(encoder, datum, &event);
int result = snd_midi_event_encode_byte(encoder.get(), datum, &event);
if (result == 1) {
// Full event, send it.
base::AutoLock lock(out_ports_lock_);
Expand All @@ -876,7 +872,6 @@ void MidiManagerAlsa::SendMidiData(MidiManagerClient* client,
}
}
}
snd_midi_event_free(encoder);

// Acknowledge send.
AccumulateMidiBytesSent(client, data.size());
Expand Down Expand Up @@ -1389,6 +1384,13 @@ bool MidiManagerAlsa::Subscribe(uint32_t port_index,
return true;
}

MidiManagerAlsa::ScopedSndMidiEventPtr
MidiManagerAlsa::CreateScopedSndMidiEventPtr(size_t size) {
snd_midi_event_t* coder;
snd_midi_event_new(size, &coder);
return ScopedSndMidiEventPtr(coder);
}

#if !defined(OS_CHROMEOS)
MidiManager* MidiManager::Create(MidiService* service) {
return new MidiManagerAlsa(service);
Expand Down
4 changes: 4 additions & 0 deletions media/midi/midi_manager_alsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ class MIDI_EXPORT MidiManagerAlsa final : public MidiManager {
// Returns true if successful.
bool Subscribe(uint32_t port_index, int client_id, int port_id);

// Allocates new snd_midi_event_t instance and wraps it to return as
// ScopedSndMidiEventPtr.
ScopedSndMidiEventPtr CreateScopedSndMidiEventPtr(size_t size);

// Members initialized in the constructor are below.
// Our copies of the internal state of the ports of seq and udev.
AlsaSeqState alsa_seq_state_;
Expand Down

0 comments on commit 0c0059c

Please sign in to comment.