3

Very simple question but I can't find any solution on the net.

Is it possible to force a Qt app to crash?

This is only for development purposes as I'd like to experiment with the crash report.

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
Vincent Duprez
  • 3,772
  • 8
  • 36
  • 76
  • Create a pointer ```QPushButton *button``` but no instance of it. Try to call a method of ```button``` and it will crash (if I understood your question correctly) – Varius Mar 30 '16 at 20:03
  • The dereference of a null pointer will crash on pretty much any platform Qt runs on, with exception of some big iron (mainframes), unless the compiler optimizes it out (according to the standard, it can!). – Kuba hasn't forgotten Monica Mar 30 '16 at 22:06
  • On Windows you can write a DLL that crashes, then inject it into your process with MS Detours. – MrEricSir Mar 30 '16 at 22:13

3 Answers3

7

Remember, we're on StackOverflow :-)

How about:

int foo(int p) {
    return foo(p)+foo(p);
}

foo(0);

PS for something tamer and more Qt-specific, you can experiment with qFatal:

qFatal("boom");
Ilya
  • 5,377
  • 2
  • 18
  • 33
  • 1
    LOL, even if it work'ed 100% of the time, he probably can't use it for the purpose of a crash report. You have my up-vote for the creativity though :) – user1708860 Mar 30 '16 at 20:20
  • @user1708860 didn't try, but I'd say it would give a proper crash report. – Ilya Mar 30 '16 at 20:22
  • signal 6 is thrown when an internal error occurs such as stackoverflow. http://stackoverflow.com/questions/3413166/when-does-a-process-get-sigabrt-signal-6 . Which means that any allocation from this point on, including stack allocations, may not work. That makes it alot harder to make a crash report... (I can't find any other resources on this matter, so i could be horribly wrong here, it's based mostly on what wer'e doing with our signal handlers) – user1708860 Mar 30 '16 at 20:32
  • 1
    @user1708860 I've tested on my machine, I get SIGSEGV (segfault). Strange ...? – Ilya Mar 30 '16 at 20:38
  • I this that this is undefined behavior, we could both be right, and we could both be wrong. – user1708860 Mar 30 '16 at 20:40
  • On my machine, the app crashes without crash report (Mac OSX) – Vincent Duprez Mar 30 '16 at 20:44
  • I'd say not UB but implementation dependant. We're just asking for infinite resources, aren't we ? – Ilya Mar 30 '16 at 20:45
  • @VincentDuprez interesting. NB Windows here. – Ilya Mar 30 '16 at 20:45
  • @VincentDuprez I also had to mention qFatal just in case, since you're asking specifically for Qt. – Ilya Mar 30 '16 at 21:01
2

You could throw an exception and never catch it, this may or may not be what you are looking for.

Edit:

this is the cross-platform most robust solution i could find: http://en.cppreference.com/w/cpp/utility/program/raise

If you are using *nix systems, you can use the raise call.

I assume you are already platform specific as you are making a crash report, which i know of no way to make cross platform at the moment. I don't know about a similar solution that would work in a windows environment, one probably exists.

user1708860
  • 1,683
  • 13
  • 32
0

You can trigger a segfault using the signal Linux library:

#include <signal.h>

void raiseSegfault()
{
    int error = raise(SIGSEGV);
    if (error != 0) {
        std::cout << "cannot raise segfault" << std::endl;
        exit(0);
    }
}

This will reliably raise a segmentation fault or exit and will not rely on undefined behaviour.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141