Question

Is there an option in the Visual Studio 2012 C++ compiler to make it warn if you use uninitialized class members?

The RTC-Checks are not compatible with managed C++ (/clr)

Was it helpful?

Solution

What kind of data member? A pointer member variable or one that gets its constructor automatically called?

It is really up to the author to be experienced enough to be paranoid about pointers and watch their initialization, assignment and dereferencing like a hawk to make sure it's safe. No compiler or static analyzer can take the place of a competent programmer in making sure pointers are used safely.

You basically want to find these issues at compile time if possible, and at run-time only as a last resort.

For compile time tool, you do have some options that might help you:

The static analyzer that comes with Visual Studio can warn if a pointer is being used without being checked first. But it does not give the same emphasis for a pointer class member. I've seen a 3rd party static analyzer called CppCheck that does that check. Coverity (Another static analyzer) would also probably do that too. Ah, but wait, Coverity doesn't work for managed code (last I checked). And it's so expensive you probably have to sell your house, and your neighbors house to pay for it, and have a coverity engineer come to your office to take 3 days to get it installed, and then it will take 24 hours to run the analysis.

For runtime checking, I'm have no idea what alternative you might have for RTC with managed code. But it would be very Very VERY wise to minimize the amount of pure native code you expose to the /clr switch. Some programmer years ago turned that on for our product for our largest project (It had hundreds of files). Even though out of the hundreds of files in the project only 4 or 5 files used the managed code, he still turned on the switch anyways for the hundreds of other pure native files. As a result, there was thousands of crashes for years until we reversed that stupidity.

So put your code in clear managable layers. Seperate the managed C++ code from the pure native C++ code and in visual studio only turn on the /clr switch on the managed files.

And by all means use static analysis tools as much possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top