3
votes

When I execute this...

void CcrashDlg::OnBnClickedBtnCrash()
{
    char* ptr = NULL;
    *ptr = 1;
}

app just logs "Access Violation", and nothing happened.

How can I crash my MFC application? (visual studio 2010)

5
Do you have any exception handlers in place? I'm not sure, but perhaps MFC does some silly swallowing of exceptions in some contexts (like this button click).Jonathon Reinhart
Try your code using the release build.jussij
@JonathonReinhart just default generated code with my onbnclickedbtncrash().chaeyk
@jussij release does same with debugchaeyk
This might be interesting for you.Jabberwocky

5 Answers

3
votes

I believe the answer is hidden in the comments to the question, so I'm going to summarize it here - this is important stuff.

The full details are at this link: The case of the disappearing OnLoad exception – user-mode callback exceptions in x64.

Invalid operations that you would expect to crash the program don't crash in all circumstances. This includes many of the techniques outlined in the other answers.

The problem only occurs when you're using a 64-bit version of Windows and you're within a function that was called by the Windows kernel. In 32-bit versions of Windows, an exception or invalid operation could be caught from the code that called into the kernel, but in 64-bit versions of Windows this is impossible. Windows itself will catch the error at the user/kernel boundary, and ignore it! This was judged to be a better outcome than crashing the program every time, since the catch blocks that worked so well in 32-bit don't get a chance to handle the error anymore.

You should still be able to halt the process immediately using ExitProcess or TerminateProcess but I haven't tried them.

2
votes

just divide a number by zero,

int div = 1;
div--;
int cr = (any number)/div;
0
votes

This is what I use to crash my app. I think it is same as yours. In release mode also it crashes.

*(int*)0 = 1;
0
votes

Assert( NULL ) will crash your application in debug mode. I don't think you want to crash the release version but if so you can use Verifythere/both.

Other simple ways to cause crash is just use sprintfwith wrong formatting specifier for example feed a string when its expecting "%d".

0
votes
  1. abort() works on release mode

  2. App crashes when compiled win64 platform on 64bit os.