The Lone C++ Coder's Blog

The Lone C++ Coder's Blog

The continued diary of an experienced C++ programmer. Thoughts on C++ and other languages I play with, Emacs, functional, non functional and sometimes non-functioning programming.

Timo Geusch

4-Minute Read

Thud, thud, thud…

The sound of the developer’s head banging on the desk late at night.

What happened? Well, I had a requirement to make use of some smart pointers to handle a somewhat complicated resource management issue that was mostly being ignored in the current implementation, mainly on the grounds of it being slightly to complicated to handle successfully using manual pointer management. The result - not entirely unexpected - was a not so nice memory leak.

No smart pointer implementation was found lurking behind the sofa, so I bravely went where other people had gone before (and failed). I bravely ignored the status of the Sun CC support in the boost library and downloaded the latest version (1.32.0 at the time of me orginially writing this). The compiler I’m using is marked as “horribly broken” in the context of boost, but hey, I only wanted to use smart pointers so it can’t be that bad, right?

First attempts with a newer compiler (WS8/5.5) proved to be encouraging. The smart_ptr tests compiled, but a lot of them failed. After an extended printf debugging session it appears that the temporaries generated by the compiler got destroyed rather later than both the writers of the C++ standard and the boost developers expected. Employing some advanced google skillz soon brought to light that by default, the SUN compiler destroys temporaries not at the end of the statement as the standard suggests but rather when it encounters the end of the scope.

Great. In fact this shouldn’t have come as that much of a surprise as SUN makes a big song and dance about the compiler’s backward compatility. They state that code which has compiled on previous versions of the compiler will definitely still compile on the newer versions. This I found true almost all the time. Unfortunately in this particular case the feature turned into a stumbling block as the backward-compatible behaviour pretty much sabotaged the expected behaviour.

Fortunately the cure is at hand - the compiler supports a command line option (-feature=tmplife) that makes it behave like every other modern C++ compiler on the face of the earth. And hey presto, the tests suddenly pass. Well, obviously those that are supposed to pass!

Unfortunately the current compiler used in the production environment is 5.3/WS6.2, not 5.5/WS8. At least it also does support the tmplife feature, so I’m obviously only a stone’s throw away from getting working smart pointers, right?

Wrong. The smart pointer’d code did compile, but did it link? Of course not, that would be too easy. So back to the tests, but this time armed with the old compiler. The older SUN compilers use a template instantiation database (the infamous SunWS_cache direcotry) to store the object code resulting of the compiler instantiating templates. For some reason, the compiler or linker fail to pull in the necessary object code for the smart pointer externals and all that. Grrr. Closer inspection of the compiler’s man page suggested that the compiler can be convinced to put this information into the object file instead (using -instances=static instead of the default behaviour). This behaviour is the default on the 5.5 compiler, but optional in the 5.3 compiler…

So finally, the smart_ptr test successfully complete using the Sun 5.3 C++ compiler. And the application – with a bit more tweaking – is leaking considerably less memory. The joy of small victories.

Playing with SunStudio 11

This is by no means a review of SunStudio 11, even though I’ve used it for production software. There’s an awful lot of power in the IDE but I’m one of those old-skool guys who have spent a lot of time learning and customising XEmacs and it’s still the editor I’m most comfortable with, so why change? For that reason, I’ve only ever used the IDE for debugging, for which it seems to be decent enough. As it’s written in Java as so many IDEs are these days (cue Eclipse) it’s not exactly the fastest IDE I’ve ever worked with but once it’s loaded up and running it appears to be decent enough.

The compiler is however a big step forward from just about any of the older SUN compilers I’ve used. It still has some quirks but there is comparative small number of them, so for most applications it now really looks like a proper standard C++ compiler, which is a big improvement over the previous efforts. Yes, there are still some quirks (a couple of them still show up in Boost) but it’s more unlikely that you stumble across those.

Overall I’d say that whichever compiler version you’re currently using, you should probably upgrade to this one. At least if you’re interested in writing reasonably modern C++, that is.

Recent Posts

Categories

About

A developer's journey. Still trying to figure out this software thing after several decades.