我正在读取C ++中的wav文件,该文件仅在发布模式下运行程序时崩溃。
我收到此错误消息:
对象0x7fc7b3d2b1f8的错误:释放对象的校验和不正确 - 对象可能在被释放后被修改。"或"分段错误:11`
在调试模式下运行程序时,它可以正常读取和播放wav文件。
void WaveFormat::Load(std::string filename) {
std::ifstream file(filename, std::ifstream::binary);
if (file.good()) {
file.read(chunkID, WaveFormat::NUM_CHARS);
file.read(reinterpret_cast<char *>(&chunkSize),
sizeof(unsigned int));
file.read(format, WaveFormat::NUM_CHARS);
file.read(subChunkID, WaveFormat::NUM_CHARS);
file.read(reinterpret_cast<char *>(&subChunkSize),
sizeof(unsigned int));
file.read(reinterpret_cast<char *>(&audioFormat),
sizeof(unsigned short));
file.read(reinterpret_cast<char *>(&numChannels),
sizeof(unsigned short));
file.read(reinterpret_cast<char *>(&sampleRate),
sizeof(unsigned int));
file.read(reinterpret_cast<char *>(&byteRate),
sizeof(unsigned int));
file.read(reinterpret_cast<char *>(&blockAlign),
sizeof(unsigned short));
file.read(reinterpret_cast<char *>(&bitsPerSample),
sizeof(unsigned short));
do
{
file.read(subChunk2ID, WaveFormat::NUM_CHARS);
file.read(reinterpret_cast<char *>(&subChunk2Size),
sizeof(unsigned int));
data = new char[subChunk2Size];
file.read(reinterpret_cast<char *>(data), subChunk2Size);
}
while (strncmp(subChunk2ID, "data", 4) != 0);
file.close();
}
}
标题文件:
struct WaveFormat
{
public:
static const unsigned short NUM_CHARS = 4;
public:
WaveFormat() : data(nullptr) {
}
~WaveFormat() {
delete[] data;
}
void Load(std::string filename);
char chunkID[NUM_CHARS];
unsigned int chunkSize;
char format[NUM_CHARS];
char subChunkID[NUM_CHARS];
unsigned int subChunkSize;
unsigned short audioFormat;
unsigned short numChannels;
unsigned int sampleRate;
unsigned int byteRate;
unsigned short blockAlign;
unsigned short bitsPerSample;
char subChunk2ID[NUM_CHARS];
unsigned int subChunk2Size;
char* data;
};
如果我注释掉了......当程序不再崩溃时。