当我使用 MTrace 检测 ALSA 官方网站上的 ALSA 示例项目中的内存泄漏时,它显示了很多内存泄漏信息。
示例项目的网站在这里!
我只是添加了一些 MTrace 代码。
#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;
}
然后使用命令编译为打击:
g++ pcm_min.cc -g -lasound -o alsa_test
执行 alsa_test:
$ export MALLOC_TRACE=/tmp/t
$ ./alsa_test
$ mtrace ./alsa_test $MALLOC_TRACE
然后它将显示如下内存泄漏信息:
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
.........
是谁的问题?追踪?阿尔萨斯?为什么?