I am still working on the Vivace tutorial. I'm working with sound now. Due to an oversight on my part I didn't catch the file downloaded didn't have the same name as the code. The program would drop into a "must_init" function design to cut down on repetitive coding. The program wouldn't print via the printf command so I didn't realize the program was crashing within this function. The Include section included stdio.h but I also included cstdio. It solved the print problem but not the crash/hang.
It appears the exit(1) command is where the program is stalling. I have to close the program within the IDE to get it to clear out. There is no window or console that pops up. This also made it a pain to run down.
Having said all of that, my question is, why would exit(1) stall? Is there a better command? I'm compiling with g++.
Reading over the code, could the problem with the function being void, yet exit appears to be trying to return something?
If you are interested here are the snippets of code involved
Thanks
If not too big, post the entire file. Got to be something else. Nothing looks wrong with function. Except, I'd make it cleaner.
Here is the code. Keep in mind
1- The name of the sound file is still wrong, for the sake of the failure
2- It's a little patch work because we started with a working program then was asked to replace sections as the lessons progressed.
3- I added iostream so I could use std::cout. I haven't found a good "step" IDE since MSQuick C. So I use std::cout to see if a program is making it to a given point or what variable values are at that point.
4- allcolor_A5.h is my own file for colors obliviously. This way I don't' have to al_map_rgb(0,255,0), I can just use BR_GREEN.
At which step is it freezing? When it is loading the file?
I'm staring at it and don't see anything. I can test it later tonight when I'm home. I don't normally use exit.
cstdio over stdio.h if you are using C++.
The difference (which is neither minor nor a matter of style) is that stdio. h and other C-like libraries when imported in a C++ file may pollute the global namespace, while the corresponding C++ headers (cstdio, cstdlib, cassert) place the corresponding functions, variables, etc., in the std namespace.
The program gets down to
ALLEGRO_SAMPLE* elephant = al_load_sample("elephant.wav");
must_init(elephant, "elephant");
It attempts to load the sound file. Then enters must_init. While in must_init, it gets down past printf but doesn't come back out of the function. There is only one command after printf, exit(1) so I assume that's where it's stalling.
void must_init(bool test, const char *description)
{
if(test) return;
printf("couldn't initialize %s\n", description);
exit(1);
}
All the other inits are successful so it never gets to exit(1).
I don't think it's the exit, but for testing purposes. Add another printf test string after the exit before the function ends.
Daniel, don't waste a lot of time on it. I was just curious if there was an alternative to exit(1) or if it was problematic with C++.
To your suggestion, here are my results.
Before File load
Before must_init in main
After If return in must_init
couldn't initialize elephant
After printf in must_init
If didn't make it after exit(1)
This is mine
// what are we attempting std::cout << "Initializing Allegro Library: "; if (!al_init()) { std::cout << "failed" << std::endl; return -1; } std::cout << "pass" << std::endl;
it does get messy
Personally, I don't like leaving things hanging. My shutdown function always gets called even if there is an error in init. In it, every object I allocated is checked and unallocated.
int main() { if (init() ==0) { // proceed loop(); } shutdown(): }
Thanks for the insight Daniel. I appreciate the time.
NVM
That said, avoid nasty things like exit and abort. It's easy enough to return non-zero in main.
Also, must init should also report success, not just failure.
I'm home and tested your code. It does just as expected. runs and exits after failing to load elephant.
output: "couldn't initialize elephant"
I put a wav file of my own and the program plays it when I press E. Some issues, how to exit program? You turned off the mouse cursor so I could not press the close button. You don't have any key presses that close the program either. I was able to close it pressing Alt+f4.
{"name":"613302","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/b\/cbe4db979eece65810032c1c2bb1fb22.png","w":789,"h":590,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/b\/cbe4db979eece65810032c1c2bb1fb22"}
Could be my computer at work. I ran it from home. Still Eclipse IDE but on Linux. It worked just like you said. Must have just been the computer at work. Doesn't make sense.
The lesson before was for the mouse. It was showing how to capture it in the window. On my work (Windows 10) I can Alt Tab to another window and the mouse will go with me. On Linux I almost had to reboot the computer. No matter which program I tabbed to, the mouse stayed with the elephant program.
As for the Esc key, the code has been so jumbled up, I'm not sure why it doesn't work but it didn't.
In any case there doesn't seem to be anything wrong with exit(1) which was my first question. Sorry if I wasted your time. Thanks again for the help.
If your program was hanging, it was probably because of a loose thread that was still running. Probably allegro's audio or window thread.
Could be Edgar.
I still think it's something to do with Windows /Eclipse IDE / Mingw32. Or a combo there of. Here is why I say that
- I can run the program from outside of the IDE and it will print the "could not init XXXX" line and quit.
- Moving too quick, I duplicated my mistake with the Gameplay code, as the audio files all started with audio_XXXXXX. It was a new program and it did the same thing. No printing and it would just hang. I've have to check a box in Eclipse to get it to stop. Fortunately this time I only made the mistake once .
Thanks for the follow up.
If you're using the same code as before, you haven't shutdown audio or allegro itself.
This code is horrible to me; why call every library initialisation method and then pass an optional null into a void method with a null as bool? Why not just have a setup function that returns a bool result and determine whether or not to continue or not based on setup success?
Yeah, I didn't create the code so I can't say why they did what they did. I figured the programmer knew more about it than I did. It seems to work well enough if the ducks are in a row.