gpt4 book ai didi

c++ - 错误显然是由尚未执行的代码引起的

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:55 28 4
gpt4 key购买 nike

我正在通过编写将 MIDI 文件转换为 Lilypond 的程序来学习 C++源文件。我的程序由两个主要部分组成:

  • 一个 MIDI 文件解析器,它创建一个名为 MidiFile 的对象。
  • 采用 MidiFile 对象并将其转换为 Lilypond 源的转换器。

今天我开始编写转换器,当我测试它时发生了一个奇怪的错误:程序在抛出异常后死亡,更具体地说是 HeaderError,这意味着 MIDI 文件中的头 block 不是预期的。看起来并没有那么奇怪,但是只有当我在错误代码之后添加一行代码时才会出现此错误!我添加了 main() 函数以更好地解释我自己

#include <iostream>
#include "midiToLyConverter.hpp"

int main(){

// a queue to store notes that have not yet been shut down
using MidiToLyConverter::Converter::NoteQueue;
// representation of a note
using MidiToLyConverter::Converter::Note;
// the converter class
using MidiToLyConverter::Converter::Converter;
// the midifile class
using Midi::MidiFile;
// representation of a midi track
using Midi::MidiTrack;
// representation of a midi event
using Midi::MidiEvents::Event;

Parser::Parser parser = Parser::Parser(); // parser class
parser.buildMidiFile(); // builds the midi file from a .mid
Midi::MidiFile* midiFile = parser.getMidiFile(); // gets the MidiFile object

// iterates over all the tracks in the MidiFile
while(midiFile->hasNext()){
std::cout<< "==========\n";
MidiTrack* track = midiFile->nextTrack();
// iterates over all events in a track
while(track->hasNext()){
Event* event = track->nextEvent();
if (event->getEventType() == Midi::MidiEvents::NOTE_ON ||
event->getEventType() == Midi::MidiEvents::NOTE_OFF
)
// print the event if it's a note on or off
event->print();
}
}

return 0;
}

像这样使用我的 main(),一切正常,但是,如果我在 buildMidiFile 和 while 循环之间添加一些东西,函数 buildMidiFile 会抛出异常!!!即使它是一个完全不相关的指令!

#include <iostream>
#include "midiToLyConverter.hpp"

int main(){

using MidiToLyConverter::Converter::NoteQueue;
using MidiToLyConverter::Converter::Note;
using MidiToLyConverter::Converter::Converter;
using Midi::MidiFile;
using Midi::MidiTrack;
using Midi::MidiEvents::Event;


Parser::Parser parser = Parser::Parser(); // parser class
parser.buildMidiFile(); // THE EXCEPTION IS THROWN HERE
Midi::MidiFile* midiFile = parser.getMidiFile(); // gets the MidiFile object

// adding this causes the exception to be thrown by the function
// buildMidiFile() called 5 lines above!
std::vector<bool>* vec = new std::vector<bool>();

// iterates over all the tracks in the MidiFile
while(midiFile->hasNext()){
std::cout<< "==========\n";
MidiTrack* track = midiFile->nextTrack();
// iterates over all events in a track
while(track->hasNext()){
Event* event = track->nextEvent();
if (event->getEventType() == Midi::MidiEvents::NOTE_ON ||
event->getEventType() == Midi::MidiEvents::NOTE_OFF
)
// print the event if it's a note on or off
event->print();
}
}

return 0;
}

我自己无法解释这是怎么可能的。因此,如果有人有想法或建议,我们将不胜感激 :) 如果有帮助,我可以发布其他类和/或函数的源代码。

最佳答案

解决了!正如对该问题的评论所指出的那样,这是由某种内存损坏引起的问题。正如我所建议的那样,我使用了一个内存检查器(valgrind)并发现这是一个非常愚蠢的错误:我只是忘记了在 for 循环中初始化一个变量,比如

for (int i; i < limit ; i++)

这导致了那个奇怪的错误 :-) 将 i 初始化为 0 解决了这个问题,现在程序可以使用放置在堆栈或堆上的 Parser 对象。

所以我建议其他遇到类似问题的人使用内存检查器来控制他们程序的内存使用情况。使用 valgrind 非常简单:

valgrind --leak-check=yes yourProgram arg1 arg2

其中 arg1 和 arg2 是您的程序需要的(最终)参数。

此外,使用 -g 标志编译您的程序(至少在 g++ 上,我不知道在其他编译器上),valgrind 还会告诉您发生内存泄漏的代码行。

感谢大家的帮助!

问候
马特奥

关于c++ - 错误显然是由尚未执行的代码引起的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6572915/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com