I was wondering why the destructors of the classes SoundIC7410 and SoundSoft66 are not called at the end of the scope.
int main(int argc, char *argv[]) { vector <Sound*> soundlist; soundlist.push_back( new SoundIC7410{argv[1]} ); soundlist.push_back( new SoundSoft66{argv[2]} ); argc = 1; /* just for the next line */ Glib::RefPtr < Gtk::Application > app = Gtk::Application::create(argc, argv, "org.gtkmm.example"); MyWindow win(soundlist); return app->run(win); }
% ./Rig011 hw:2,0 hw:1,0 /dev/ttyUSB1 &> log.txt % tail log.txt Sound::asound_read(): loop_count = 2, frames_actually_read = 2048 Sound::asound_read(): in the while loop, avail = 152
The answer is here:http://www.stroustrup.com/bs_faq2.html#delete-scope
That is, there was some (mistaken) assumption that the object created by “new” would be destroyed at the end of a function.
If you want an object to live in a scope only, don’t use “new” but simply define a variable.
So if I try:
int main(int argc, char *argv[]) { vector <Sound*> soundlist; SoundIC7410 sound1(argv[1]); SoundSoft66 sound2(argv[2]); soundlist.push_back(&sound1); soundlist.push_back(&sound2); argc = 1; /* just for the next line */ Glib::RefPtr < Gtk::Application > app = Gtk::Application::create(argc, argv, "org.gtkmm.example"); MyWindow win(soundlist); return app->run(win); }
We get:
% ./Rig011 hw:2,0 hw:1,0 /dev/ttyUSB1 &> log.txt % tail log.txt Sound::asound_read(): loop_count = 2, frames_actually_read = 2048 Sound::asound_read(): in the while loop, avail = 112 SoundSoft66::~SoundSoft66() destructor.. SoundIC7410::~SoundIC7410() destructor..
Or more simply, try this:
int main() { Test test10, test11; Test* test20 = new Test; Test* test21 = new Test; cout << "test10 = " << &test10 << ", test11 = " << &test11 << endl; cout << "test20 = " << test20 << ", test21 = " << test21 << endl; return 0; } Test::Test() { cout << "aaa \n"; } Test::~Test() { cout << "zzz \n"; }
aaa aaa aaa aaa test10 = 0x7ffd56299f50, test11 = 0x7ffd56299f60 test20 = 0x1f8b010, test21 = 0x1f8b030 zzz zzz