Reputation: 11
When I used MTrace to detect memory leak in ALSA sample project which is at ALSA's official website, it shows a lot of memory leak info.
The sample project's website is Here!
I just add some MTrace code.
#include <alsa/asoundlib.h>
#include <mcheck.h>
static char *device = "default"; /* playback device */
snd_output_t *output = NULL;
unsigned char buffer[16*1024]; /* some random data */
int main(void)
{
mtrace();
int err;
unsigned int i;
snd_pcm_t *handle;
snd_pcm_sframes_t frames;
for (i = 0; i < sizeof(buffer); i++)
buffer[i] = random() & 0xff;
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = snd_pcm_set_params(handle,
SND_PCM_FORMAT_U8,
SND_PCM_ACCESS_RW_INTERLEAVED,
1,
48000,
1,
500000)) < 0) { /* 0.5sec */
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
for (i = 0; i < 16; i++) {
frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
if (frames < 0)
frames = snd_pcm_recover(handle, frames, 0);
if (frames < 0) {
printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
break;
}
if (frames > 0 && frames < (long)sizeof(buffer))
printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
}
snd_pcm_close(handle);
return 0;
}
Then use command to compile as blow:
g++ pcm_min.cc -g -lasound -o alsa_test
Execute alsa_test:
$ export MALLOC_TRACE=/tmp/t
$ ./alsa_test
$ mtrace ./alsa_test $MALLOC_TRACE
Then it will show memory leak info like this:
Memory not freed:
-----------------
Address Size Caller
0x000000000075b450 0x10 at 0x7f8ff4f728e8
0x000000000075b470 0x20 at 0x7f8ff4f72904
0x000000000075b4a0 0x10 at 0x7f8ff5249373
0x000000000075b500 0x20 at 0x7f8ff5257961
0x000000000075b530 0x1a at 0x7f8ff4c0248a
0x000000000075b560 0x48 at 0x7f8ff4f6c899
0x000000000075b6c0 0x25 at 0x7f8ff524cef4
0x000000000075b6f0 0x6 at 0x7f8ff4f6c9e5
0x000000000075b710 0x48 at 0x7f8ff4f6c899
0x000000000075b760 0x5 at 0x7f8ff4f6c9e5
0x000000000075b780 0x41 at 0x7f8ff524cef4
0x000000000075b7d0 0x28 at 0x7f8ff524cef4
0x000000000075b800 0x20 at 0x7f8ff466b627
.........
It is whose problem? MTrace? ALSA? Why?
Upvotes: 1
Views: 29