user1343318
user1343318

Reputation: 2141

What happens if you allocate memory in a process once it has crashed?

I have been working with Google breakpad for crash reporting for sometime. One of the things that it emphasizes is that one should not allocate memory once a process has crashed. It says it isn't 'safe' to allocate memory in a process once it has crashed.

What exactly is the meaning of 'safe' here?

Upvotes: 3

Views: 252

Answers (3)

Chris J. Kiick
Chris J. Kiick

Reputation: 1505

You really shouldn't be doing anything too complicated after a crash, lest you hang or have a crash in your crash code. The two main reasons are: locks and global variables. If your process crashed while holding a lock, and you need that lock for whatever you are doing after the crash, it'll either error off, or worse, hang. There are also lots of state information stored in global variables. Not just what's declared in your program, but also lots of things used by libraries and even the dynamic linker/loader. If any of those are bad, the result of calling a function that uses one of those globals is unpredictable.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726779

Once a process has crashed, the best you can hope for is learning what has happened before exiting the process for good. The "safety" is, therefore, reduced to your error reporting code not causing a crash of its own on top of the original crash. That is why your options at this point are limited: for example, trying to allocate memory is dangerous, because the original crash could have been caused by corrupted heap.

Upvotes: 3

egur
egur

Reputation: 7970

It's not safe because the standard library may be in a corrupt state, causing a second crash when you for more memory.

If you want to print something, do it w/o memory allocation (e.g. use local variables)

Upvotes: 4

Related Questions