- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在通过编写将 MIDI 文件转换为 Lilypond 的程序来学习 C++源文件。我的程序由两个主要部分组成:
今天我开始编写转换器,当我测试它时发生了一个奇怪的错误:程序在抛出异常后死亡,更具体地说是 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/
对包含as、bs、cs的字符串,我们可以进行如下操作。我们可以取任意两个相邻的不同字符并将它们替换为第三个。例如,“ab”被缩减为“c”,“ba”也是如此,依此类推。我编写这段代码是为了对 t 个字符
我得到了数据库检索字符串的不同哈希值和通过 html 表单提交的相同值。 我做错了什么? 数据库: SET NAMES utf8; SET foreign_key_checks = 0; SET ti
我正在继续那个加密程序,现在我正在为它开发 GUI。不过,我有一个奇怪的问题:在主菜单中,我有调用加密和解密子程序的按钮,但它们显然是在按下按钮之前执行的。 定义.py #!/usr/bin/env
我最近升级了 PyCharm(社区版)。如果重要的话,我正在 Mac OSX 机器上运行。升级后,我有一个项目,PyCharm 找不到任何 python 模块。它找不到 numpy,matplotli
我开始调整我的 spring boot 项目以使用 jpa/hibernate。在这个阶段,我只想检索 mysql 数据库中表的 id。 以下是相关类: @SpringBootApplication
我找不到任何类似的问题,我不知道那里会发生什么(也许可能是愚蠢的事情),但我还没有找到任何关于可能发生的事情的线索。 我有这个数组: const superVillains = [ { value
我正在使用 ExecutorService 解决使用生产者-消费者问题的事务轮询案例和 BlockingQueue .我有一个交易 list ,我想对其进行验证并采取行动。我得到一个新的交易来不断地验
注意这涉及 HTML 电子邮件编码(有限制) 例如,在我使用的 Mail Chimp 电子邮件模板中有这样的内容: 我猜他们是在覆盖基础,但什么时候应该使用 valign="top" 什么时候使用
我有一个名为 Component 的基类,它有许多派生自它的类。我希望每个类都有一个与之关联的整数(哪个组件获得什么值无关紧要,只要它们从 0 开始并且是连续的)。我不知道如何直接执行此操作,因此在与
我对 python 相当缺乏经验,所以我发现自己不知道如何解决这个错误。我继承了一个 python 应用程序,它主要只是将文件和文件夹从一个地方复制到另一个地方。所有文件和文件夹都在本地机器上,用户拥
我一生都无法弄清楚为什么它一直显示“无法找到显式 Activity 类 com.example.timer.beep。您是否在 Android list 中声明了该 Activity ?”。据我所知,
许多有关多处理使用的教程似乎并没有完全解决为什么下面的技术适用于线程但不适用于多处理。 为什么这不适用于多处理,以及我尝试要做的事情的实现是什么?谢谢! 线程实现(工作正常,对我来说很有意义): fr
我有一个非常奇怪的问题 - 我使用 firebase 验证自己的身份(通过电话并使用电子邮件/密码凭据链接帐户),并且它工作得很好(获取数据等)。 当应用程序(出于任何原因)对我崩溃时,我不断收到权限
在我的应用程序中(只显示相关代码),我有一个类 Test 有一个属性 var location: CLLocation 我使用 存档它 public func encode(with aCoder:
环境:我在装有 High Sierra 的 Mac 上使用 conda 4.6.7。 我有一些遗留的 Python 代码,首先我想运行它。当然,由于这是研究代码,我没想到会找到“花哨的东西 (!!)”
我看过很多关于这个主题的问题和答案,但似乎没有一个对我的情况有帮助。我的 PHP 代码成功创建了一个新的日志文件,但随后无法访问该文件以附加更多信息、关闭它等。 我正在将应用程序从本地 XAMPP 迁
我使用 000Webhost。 我刚刚在我的 MySQL 数据库中创建了一个表。 我遇到了这个错误: “mysql.proc 的列数错误。预期为 20,实际为 16。表可能已损坏”。 我的其他表工作正
以下查询: SELECT DISTINCT ClassName FROM SiteTree ORDER BY ClassName 没有明显的顺序返回! 无论我引用列/表名称,使用或不使用 DISTIN
我正在学习 mysql 中的存储过程和游标,我偶然发现了它: delimiter // CREATE PROCEDURE some_func() BEGIN DECLARE link_rewr
我正试着用“文本编码标准”来包装我的大脑。当将一堆字节解释为“文本”时,必须知道哪个“编码sheme”适用。我知道的可能的候选人: ascii:非常基本的编码方案,支持128个字符。 cp-1252:
我是一名优秀的程序员,十分优秀!