Author Topic: "optimize even more (for speed)" flag crashes application  (Read 7096 times)

Offline ValeV

  • Multiple posting newcomer
  • *
  • Posts: 23
"optimize even more (for speed)" flag crashes application
« on: April 01, 2019, 11:44:50 am »
Hey,

I am developing GUI program in CodeBlocks using mingw compiler with wxWidgets library. I had troubles running standalone .exe file to run application, becuase it crashed when I exited it (just in Release version, and not in Codeblocks IDE or debug version). I seemed to fix it by unchecking "Optimize even more (for speed) [-O2]" flag, under build options->compiler settings->compiler flags->optimization.

Is this something that is known and if yes, can I get some additional info on this? Any help on why this might be the problem?

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: "optimize even more (for speed)" flag crashes application
« Reply #1 on: April 01, 2019, 01:00:45 pm »
It would be easier to help if you specify MinGW and wxWidgets versions, but in any case the problem does not seem related to C::B.

Offline ValeV

  • Multiple posting newcomer
  • *
  • Posts: 23
Re: "optimize even more (for speed)" flag crashes application
« Reply #2 on: April 01, 2019, 01:53:49 pm »
It would be easier to help if you specify MinGW and wxWidgets versions, but in any case the problem does not seem related to C::B.

True, it may be wxWidgets problem. That's why I made a thread on wxWidgets forum as well, asking for a possible explanation.

Anyway, mingw version: 6.3.0-1
wxWidgets: 3.1.2

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: "optimize even more (for speed)" flag crashes application
« Reply #3 on: April 01, 2019, 05:46:37 pm »
Works in Debug mode but crashes in Release mode? Chances are very low that this is caused by a third party library or even the compiler and very high that it is caused by you :).

Probably a buffer overrun or something related.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: "optimize even more (for speed)" flag crashes application
« Reply #4 on: April 01, 2019, 06:13:01 pm »
I had crashes in release mode when clicking on comboboxes in property grids && the program was linked with wxWidgets 3.1.0 DLL. They didn't happen neither in debug mode nor with static linking, and they went away with wxWidgets 3.1.1 without changing the code. The compiler was TDM-GCC 5.1.0.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: "optimize even more (for speed)" flag crashes application
« Reply #5 on: April 01, 2019, 07:01:05 pm »
There was a few optimization bugs in wxWidgets headers; no idea when they got fixed.
This problem sounds like that problem.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline ValeV

  • Multiple posting newcomer
  • *
  • Posts: 23
Re: "optimize even more (for speed)" flag crashes application
« Reply #6 on: April 02, 2019, 11:55:23 am »
Works in Debug mode but crashes in Release mode? Chances are very low that this is caused by a third party library or even the compiler and very high that it is caused by you :).

Probably a buffer overrun or something related.

It works in Release as well, when I remove said flag. Could removing the flag fix the error like buffer overrun?

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: "optimize even more (for speed)" flag crashes application
« Reply #7 on: April 02, 2019, 12:21:04 pm »
Quote
Could removing the flag fix the error like buffer overrun?

It won't fix the issue, just hide it so it will bite you again in the future. Buffer overrun side effects depend heavily on memory layout, and it changes with optimization level, debug mode, statixc or dynamic linking and compiler versions.

Offline ValeV

  • Multiple posting newcomer
  • *
  • Posts: 23
Re: "optimize even more (for speed)" flag crashes application
« Reply #8 on: April 02, 2019, 12:59:24 pm »
Quote
Could removing the flag fix the error like buffer overrun?

It won't fix the issue, just hide it so it will bite you again in the future. Buffer overrun side effects depend heavily on memory layout, and it changes with optimization level, debug mode, statixc or dynamic linking and compiler versions.

But the program crashed even if I just opened and quickly closed it. I didn't do any other operations. How can buffer overrun happen?

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: "optimize even more (for speed)" flag crashes application
« Reply #9 on: April 02, 2019, 01:18:27 pm »
Don't know without seeing the code. If it does not crash in debug mode you can't debug properly, so you can try instead inserting wxMessageBox calls in the program flow to see which one shows before the crash.

Quote
I just opened and quickly closed it

If it fails at closing may be a window is not properly destroyed. Anyway this is out of this forum's scope.

Offline Krice

  • Almost regular
  • **
  • Posts: 150
Re: "optimize even more (for speed)" flag crashes application
« Reply #10 on: April 03, 2019, 09:11:28 am »
But the program crashed even if I just opened and quickly closed it. I didn't do any other operations. How can buffer overrun happen?

Why can't it happen if it's in the code that runs when you run the program. Buffer overruns are not the only thing that can mess up memory management. Memory allocations do that, too.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: "optimize even more (for speed)" flag crashes application
« Reply #11 on: April 03, 2019, 09:24:26 am »
And use-after-free, very frequent when closing applications.

Offline ValeV

  • Multiple posting newcomer
  • *
  • Posts: 23
Re: "optimize even more (for speed)" flag crashes application
« Reply #12 on: April 04, 2019, 07:57:26 am »
And use-after-free, very frequent when closing applications.


But the program crashed even if I just opened and quickly closed it. I didn't do any other operations. How can buffer overrun happen?

Why can't it happen if it's in the code that runs when you run the program. Buffer overruns are not the only thing that can mess up memory management. Memory allocations do that, too.

Thank you for suggestions. Though I still don't understand how "optimize even more" flag in CodeBlocks would hide/fix the problem?

Offline Krice

  • Almost regular
  • **
  • Posts: 150
Re: "optimize even more (for speed)" flag crashes application
« Reply #13 on: April 04, 2019, 09:05:09 am »
Though I still don't understand how "optimize even more" flag in CodeBlocks would hide/fix the problem?

Optimization changes the memory layout of the program which can make some bugs crash the program. Or it crashes in different place. When you debug memory management crashes they often don't stop at the exact point of error, but later. Sometimes libraries can be blamed, because they force you to conform some ugly rules. I think wxwidgets is one of those, because as far as I know it has some kind of special memory management going on with smart pointers. In my opinion smart pointers should never have been added in C++, because it's just not the way C++ works. There is nothing wrong with manual memory management if it doesn't have bugs, obviously.

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: "optimize even more (for speed)" flag crashes application
« Reply #14 on: April 04, 2019, 01:12:39 pm »
There is nothing wrong with manual memory management if it doesn't have bugs, obviously.
Human is a bug that cant be fixed ;D