Program crashes with optimization level O2

I am experiencing a difficulty undersatnding why my program (C++, HP UNIX) crashes. It crashes only when I build it with -O (+O2) optimization switch (used in aCC compiler). It works ok with +O0 or +O1 optimization.

Also, I see that local variables are shown incorrecly when program is built with +O2 optimization switch. They show correcly with +O0 or +O1 optimization.

Could anyone advise me on the +O2 optimization? Why this could lead to the crash? Why the debugger fails to show correctly the local variables?

It is compiled with the following switches:
/opt/aCC/bin/aCC -AA -DHPUX -DBUILD_UNIX -DNO_MSG_BUILD -DNO_MAP_BUILD -DUNICODE -DPIC +Z +inst_auto -D_THREADSAFE -mt +W641 +W829 -g0 -O -I.. -I../.. -I../lib -I/usr/local/include/stl -I../pcmsrv32 -I../pcmlib -g0 -c

Below is the gdb output after the crash:

Program received signal SIGSEGV, Segmentation fault.
0x7f0cc484 in GetAddress (grid=0, link=0, rAddress=@0x0) at street.cpp:352
352 street.cpp: No such file or directory.
(gdb) backtrace
#0 0x7f0cc484 in GetAddress (grid=0, link=0, rAddress=@0x0) at street.cpp:352
#1 0x7f2409d8 in LRAddrOutput::AddressSearch (this=0x7f7f41e8, pInterp=0x0,
grid=13, oldNumMatches=2132813148, rLinkJuris=@0x7f7f41cc,
rPostalCodes=@0x7f7f41e8, rLinkInfo=@0x7f7f4178, rNameInfo=@0x7f7f4194,
exact=@0x7f7f4078) at geoaddrs.cpp:3845
#2 0x7f23e450 in LRAddrOutput::SearchForAddress (this=0x7f7f3fe0, gridID=0,
pInterp=0x401be934, exact=@0x7f129158) at geoaddrs.cpp:2887
#3 0x7f232b88 in LRAddrOutput::CalcMatches (this=0x0, pCB=0x0)
at geoaddrs.cpp:559
#4 0x7f244664 in LRAddr::Lookup (this=0x694, region=0x0,
state=0x3 <Error reading address 0x3: Invalid argument>, pCB=0x0)
at geoaddrs.cpp:4901
#5 0x7f24beb0 in CGeocoder::FindStreetMatchesW (this=0x4004cd7c,
addrW=0x4004cd38 "\177\026\357\b", cityW=0x401be3a4 "", stateW=0x0,
countyW=0x401be934 "", zipW=0x401be9b4 "", size=0 '\000', grid=7026188,
pCB=0x0, type=0, lLat=0, lLong=0) at geocoder.cpp:1044
#6 0x7f2a21d4 in Place_NumAddressMatches (geoID=2139042812,
inStr=0x7f7f33f0 "",
city=0xffffffff <Error reading address 0xffffffff: Bad address>,
state=0x0, county=0x401be934 "", zip=0x401be9b4 "", size=0 '\000',
grid=7026188, UpdateProgMeter=0) at placemgr.cpp:957
#7 0x7f30d5d4 in GeoCodeImpl::NodeForCityOrAddr (this=0x7f7f3300,
pGeoResults=0x7f2b8430, pszLookup=0x4000f608 "NA", bExactMatch=96,
bSPLC=false, bRdNameOnly=false) at geo_impl.cpp:542
---Type <return> to continue, or q <return> to quit---
#8 0x7f30df4c in GeoCodeImpl::Nodefor (this=0x401be370,
pszLookup=0x3 <Error reading address 0x3: Invalid argument>,
bExactMatch=-56, returnSPLC=96, pRtOptions=0x40009e78,
bTrySupressPicklist=false) at geo_impl.cpp:687
#9 0x7f336774 in RouteImpl::AddStop (this=0x401b7f58,
name=0x400c17ec "\177+g@t\nfin=CoPilot tiedostotiedot\nfre=Informations sur
le Fichier CoPilot\nger=CoPilot Dateiinformationen\ngre=\316\240\316\273\316\267
\317\201\316\277\317\206\316\277\317\201\316\257\316\265\317\202 \316\263\316\27
1\316\261 \317\204\316\277 \316\261\317\201\317\207\316\265\316\257\316\277 CoPi
lot\nita=Informazioni sui file di CoPilot"..., gc=0x7f7f31b0)
at route_impl.cpp:488
#10 0x7f39915c in PCMSAddStop (trip=333240,
stop=0x5159c "PCMSAddStop returned %d\n") at pcmsrvapp.cpp:1312
#11 0x266b8 in TestFuelOpt () at pcmstest.cpp:616
#12 0x25ae4 in main () at pcmstest.cpp:467
(gdb)

Any help is appreciated.

Often optimizations are incompatible with debuggers because optimizers will re-organize variables on the stack for efficiency, um, basically optimizing the thing.

Optimizers are more aggressive and less forgiving of dodgy code.

Also, if you are using C++ libraries, have these been built with the same optimizations?

Use maximum warning and errors during compilation, for HPUX compilers I use "-Aa -D_HPUX_SOURCE +e +We".

Try GCC and see if that exposes anything, again use "-Wall -Werror".

Thank you, porter!

I tried -Aa option. The code won't compile, it complains about the inline statements we have in our code. Apparently, there's a reason why the -AA flag is used. All libraries are built with the same options.

It's weird to see that -g option is not compatible with -O (a typical optimization option).

The aCC man page says that +O2 optimization option is the same as +O1 optimization plus global optimization. What the "global optimization" means in this case? Are there any specific things I have to check in my code for that cause the crash with +O2 option, but not +O1?

Thank you in advance.

I found that the program does not crash even with +O2 optimization when I set the inline option to zero (using "+inline_level 0" or "+d"). I understand that inline functions sometimes improve the speed but still don't see why using them could lead to a crash if everything else is OK with the program.

I also understand why the program might be not crashing now. By disabling inlined calls, I moved some code around and the problem (if it exists) could be just hidden now. Any ideas, or information on inline function calls with aCC in HP UNIX? Anyone has experience when making function inline would lead to a crash or knows about problems aCC has with inline calls?

Thanks.