I'm sorry, I misunderstood what I've done here, and seem to have found what the problem is. It's not during the closing and opening stage, it's something else:
If I make any changes to the code, the project is still not saved. However if I want to test the code and I click the Run button, Lazarus automatically saves the project without asking me if I want to save the project or not. This is really ackward, as sometimes I want to just test a peace of code before I decide to save my project. And if Lazarus crashes while trying to compile, the old peace of code is permanently lost (since after a crash you can't use the Undo option).
As a workaround I guess I could wrap the peace of code I want to replace with brackets {} so the compiler ignores them (as it ignores comments), but this really isn't an efficient way to backup and replace a peace of code.
Wouldn't it be better if Lazarus created a temporary project file when it needs to compile the project which was recently edited?