我在 OSX 上制作基于 Libsndfile 的音频应用程序时遇到了奇怪的问题。读取和写入缓冲区中的数据以奇怪且不可预测的方式损坏。
这是一个为我重现问题的简短程序:
#include <iostream>
#include "sndfile.h"
int main(int argc, const char * argv[])
{
float* buffer = (float*)malloc(4096*sizeof(float));
SNDFILE* file;
SF_INFO infos;
infos.format = 0;
file = sf_open("ABCD.WAV",SFM_READ,&infos);
if (file==NULL)
{
std::cout << "LIBSNDFILE ERROR: " << sf_strerror(file) << "\n";
}
int samplesread=1;
while (samplesread!=0)
{
samplesread = sf_readf_float(file,buffer,4096);
std::cout << " " << samplesread;
}
std::cout << "";
sf_close(file);
free(buffer);
return 0;
}
该程序编译并运行良好,但使用 Valgrind 运行它会显示这种错误:
==933== Invalid write of size 8
==933== at 0x56EF4B: _platform_bzero$VARIANT$Merom (in /usr/lib/system/libsystem_platform.dylib)
==933== by 0x2FDBB: psf_memset (in /opt/local/lib/libsndfile.1.dylib)
==933== by 0x11E0B: sf_readf_float (in /opt/local/lib/libsndfile.1.dylib)
==933== by 0x100001323: main (in ./sndfiletest)
==933== Address 0x873270 is 0 bytes after a block of size 16,384 alloc'd
==933== at 0x4711: malloc (vg_replace_malloc.c:296)
==933== by 0x100001287: main (in ./sndfiletest
提前感谢您的帮助-T