return 0; causes runtime error. Please help. ;_;



  • Hey, I am having a really really dumb problem.. I wrote a console application (with project files set to not show a window) and the code itself works so far... But every time return 0; is called, the program closes - but gives me this very annoying error message...
    For my previous console applications I always used a.exec(); but it always requires a key to be pressed while it has the focus. But as I wrote before I'm not using a console window so I can't press the button.

    So guys... Can you please give me a way to close my program without an error message occuring or a key needing to be pressed? I sadly haven't found anything with the magical power of Google... Thanks in advance. :)

    In case it should be relevant, here's my .pro file...

    (There was a .pro file here.)

    ...and my code (simplified, because "the inside" already does what it's supposed to do).

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv)
    
        ...
    
        return 0; or a.exec(); or what ever you suggest!
    }
    

    The solution can be found in the 8th post.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Looks strange indeed. What does a run through the debugger tells you ?



  • @SGaist said:

    Hi and welcome to devnet,

    Looks strange indeed. What does a run through the debugger tells you ?

    I don't really know how to use the debugger properly yet... But just klicking on the green triangle with the bug didn't make any difference. The error message occured as before and it said "terminate called without an active exception" just like it says on the picture. :/

    Thanks for the fast reply, though! I really appreciate that. :)


  • Lifetime Qt Champion

    Can you share your complete main function ?



  • TL; DR: Comments are German and the program is supposed to restart the computer and has certain control loops to abort the process if wished so.

    Yeah sure! :) My varible names are English, my comments are German tho... But I can tell you what my program roughly does. It's supposed to restart the computer every time it is executed (For testing purposes it'll restart in 166 minutes or so, so I have time to stop the process with cmd.) But I added a loop which lets me abort the process with Numpad0+Numpad1 if needed. In another thread runs a timer which will continue the program if the shortcuts aren't pressed in a certain amount of time. The length of the timer should also be dynamic - the first time it shall be 60 seconds (60000 ms) and create a file where the length of the next timer is written in (55000ms = 60000ms-5000ms). That's all for now. I'm currently working on the File part. :)

    When I made this thread this section didn't exist tho and everything worked fine (except the crash at the end!). And now with the addition of these lines obviously nothing has changed.

    But I don't wanna keep babbling all night long. ;) Here's my code:

    Edit: Finished the file part. This part should work now. (Updated the code here, too). Still having the problem with the error message, though.

    #include "Header.h"
    
    
    int main(int argc, char *argv[])
    {
        QApplication PermaRestart(argc, argv);
    
    /*Notice: I initially posted my main here. I removed it again after my problem has been solved.*/
    
        return 0;
    }
    


  • Still having the problem. Nobody able to offer a solution? :S



  • This post is deleted!


  • After a bit of expermimenting with already working console programs which use return 0;, I finally found the solution myself:

    When using std::thread it seems to be a must to use the join() function somewhere after the start of the thread. I wasn't using join() for every of my threads because I already made it finish before calling return 0; with my program flow. And how I might experience: Not using join() causes an "unexpected terminate without active exception" message.

    Confusingly I have never read about this absolute necessity of join() in all the C++ references I visited... They should add this as notice in my opinion. (Or I'm just blind, lol.)

    Moderators can mark this as solved! ^.^

    Thynome


  • Lifetime Qt Champion

    Please practice some patience, allow at least 24 hours before bumping your own thread. This forum is community driven and not all members live in the same timezone as you.

    As for your problem, first: why are you using so much heap allocated objects ? Then: what does your threads do ?


  • Lifetime Qt Champion

    Post crossed :D

    Glad you found out :)

    You can mark the thread solved yourself with the "Topic Tool" button :)

    Happy coding !



  • @SGaist Sorry I bumped it but some random Korean guy made about 2 pages of spam threads and I was afraid to get forgetten. ;)

    For the heap allocated objects: You're meaning the ones using new and delete don't you (Sorry, I started coding 6 - 7 months ago so I don't know all the professional words yet. ^^)? If you mean them, I'm using so many because in my code they completely replace my normal varibles. They just allow me more control over my program and (I know, it's absolutely not relevant in these scales.) better performance. I know about just using normal varibles and using call by reference with & if necessary but I personally find it confusing comparing to my dynamic pointers. :)


  • Lifetime Qt Champion

    He got lucky today…

    Don't worry about them.

    Better performance ? Not necessarily: you deference these pointers most of the time so it's not really what's best for performance.

    Since you're starting, take the time to study C++11, there are some pretty good books worth reading, especially if you're starting.



  • @SGaist Can you recommend some in particular? I've already read some basic ones when I learned programming C from scratch but I'm interested in your opinion. If possible they shouldn't be too hard to read because my English is still not as good as my German. ^^


  • Lifetime Qt Champion

    You can find some good references here and Effective Modern C++


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.