Error apparently raised by code not yet executed

I learn C ++ by writing a program to convert MIDI files to Lilypond source files. My program consists of two main parts:

  • a MIDI file parser that creates an object called MidiFile.
  • a converter that takes MidiFile objects and converts it to a Lilypond source.

Today I started to encode the converter, and while I was testing it, a strange error occurred: the program dies after an exception is thrown, or rather, a HeaderError, which means that the header fragment in the MIDI file is not as expected. It would seem strange, but this error only appears if I add a line of code after the buggy code! I am adding a main () function to better explain myself

#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 a note on or off event->print(); } } return 0; } 

With my main () like this, everything works correctly, but if I add something between buildMidiFile and the while loop, the buildMidiFile function throws an exception !!! Even if this is a completely unrelated instruction!

 #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 a note on or off event->print(); } } return 0; } 

I can’t explain how this is possible. Therefore, if someone has ideas or advice, all help will be very grateful :) If this is useful, I can publish the source code for other classes and / or functions.

+6
source share
1 answer

Solved! As noted in the comments on this question, this was a problem caused by some kind of memory corruption. As I said, I used the memory checher (valgrind) and found out that it was a really stupid mistake: I just forgot to initialize the variable in a for loop, something like

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

and this led to this strange error :-) Initialization I solved the problem until 0, and now the program works with the Parser object, placed either on the stack or in the heap.

Therefore, I suggest that others who encounter similar problems use a memory tester to control the memory usage of their program. Using valgrind is very simple:

 valgrind --leak-check=yes yourProgram arg1 arg2 

where arg1 and arg2 are the (possible) arguments that your program requires.

Besides compiling your program using the -g flag (at least in g ++, I don't know other compilers), valgrind will also tell you about which line of code the memory leak has occurred.

Thank you all for your help!

respectfully
Matteo

+3
source

Source: https://habr.com/ru/post/891969/


All Articles