views:

468

answers:

13

Hi everyone,

We are producing a portable code (win+macOs) and we are looking at how to make the code more rubust as it crashes every so often... (overflows or bad initializations usually) :-(

I was reading that Google Chrome uses a process for every tab so if something goes wrong then the program does not crash compleatelly, only that tab. I think that is quite neat, so i might give it a go!

So i was wondering if someone has some tips, help, reading list, comment, or something that can help me build more rubust c++ code (portable is always better).

In the same topic i was also wondering if there is a portable library for processes (like boost)?

Well many Thanks.

+1  A: 

You don't mention what the target project is; having a process per-tab does not necessarily mean more "robust" code at all. You should aim to write solid code with tests regardless of portability - just read about writing good C++ code :)

As for the portability section, make sure you are testing on both platforms from day one and ensure that no new code is written until platform-specific problems are solved.

Jedidja
+1  A: 

You really, really don't want to do what Chrome is doing, it requires a process manager which is probably WAY overkill for what you want.

You should investigate using smart pointers from Boost or another tool that will provide reference counting or garbage collection for C++.

Alternatively, if you are frequently crashing you might want to perhaps consider writing non-performance critical parts of your application in a scripting language that has C++ bindings.

Mike McQuaid
A: 

You can always add exception handling to your program to catch these kinds of faults and ignore them (though the details are platform specific) ... but that is very much a two edged sword. Instead consider having the program catch the exceptions and create dump files for analysis.

If your program has behaved in an unexpected way, what do you know about your internal state? Maybe the routine/thread that crashed has corrupted some key data structure? Maybe if you catch the error and try to continue the user will save whatever they are working on and commit the corruption to disk?

Rob Walker
+1  A: 

Scott Meyers' Effective C++ and More Effective C++ are very good, and fun to read.

Steve McConnell's Code Complete is a favorite of many, including Jeff Atwood.

The Boost libraries are probably an excellent choice. One project where I work uses them. I've only used WIN32 threading myself.

John at CashCommons
+5  A: 

The Chrome answer is more about failure mitigation and not about code quality. Doing what Chrome is doing is admitting defeat.

  1. Better QA that is more than just programmer testing their own work.
  2. Unit testing
  3. Regression testing
  4. Read up on best practices that other companies use.

To be blunt, if your software is crashing often due to overflows and bad initializations, then you have a very basic programming quality problem that isn't going to be easily fixed. That sounds a hash and mean, that isn't my intent. My point is that the problem with the bad code has to be your primary concern (which I'm sure it is). Things like Chrome or liberal use to exception handling to catch program flaw are only distracting you from the real problem.

Torlack
An application that relies heavily on external plugins (such as Chrome) has more justification for this level of protection since there is nothing they can do to fix the plug-in, whereas they can make the overall user experience better.
Rob Walker
Rob, That is a very good point.
Torlack
A: 

Beside writing more stable code, here's one idea that answers your question.

Whether you are using processes or threads. You can write a small / simple watchdog program. Then your other programs register with that watchdog. If any process dies, or a thread dies, it can be restarted by the watchdog. Of course you'll want to put in some test to make sure you don't keep restarting the same buggy thread. ie: restart it 5 times, then after the 5th, shutdown the whole program and log to file / syslog.

A: 

Build your app with debug symbols, then either add an exception handler or configure Dr Watson to generate crash dumps (run drwtsn32.exe /i to install it as the debugger, without the /i to pop the config dialog). When your app crashes, you can inspect where it went wrong in windbg or visual studio by seeing a callstack and variables.

google for symbol server for more info.

Obviously you can use exception handling to make it more robust and use smart pointers, but fixing the bugs is best.

gbjbaanb
A: 

Build it with the idea that the only way to quit is for the program to crash and that it can crash at any time. When you build it that way, crashing will never/almost never loose any data. I read an article about it a year or two ago. Sadly, I don't have a link to it.

Combine that with some sort of crash dump and have it email you it so you can fix the problem.

Echo
A: 

I would recommend that you compile up a linux version and run it under Valgrind.

Valgrind will track memory leaks, uninitialized memory reads and many other code problems. I highly recommend it.

KPexEA
+1  A: 

I agree with Torlack.

Bad initialization or overflows are signs of poor quality code.

Google did it that way because sometimes, there was no way to control the code that was executed in a page (because of faulty plugins, etc.). So if you're using low quality plug ins (it happens), perhaps the Google solution will be good for you.

But a program without plugins that crashes often is just badly written, or very very complex, or very old (and missing a lot of maintenance time). You must stop the development, and investigate each and every crash. On Windows, compile the modules with PDBs (program databases), and each time it crashes, attach a debugger to it.

You must add internal tests, too. Avoid the pattern:

doSomethingBad(T * t)
{
   if(t == NULL) return ;

   // do the processing.
}

This is very bad design because the error is there, and you just avoid it, this time. But the next function without this guard will crash. Better to crash sooner to be nearer from the error.

Instead, on Windows (there must be a similar API on MacOS)

doSomethingBad(T * t)
{
   if(t == NULL) ::DebugBreak() ; // it will call the debugger

   // do the processing.
}

(don't use this code directly... Put it in a define to avoid delivering it to a client...) You can choose the error API that suits you (exceptions, DebugBreak, assert, etc.), but use it to stop the moment the code knows something's wrong.

Avoid the C API whenever possible. Use C++ idioms (RAII, etc.) and libraries.

Etc..

P.S.: If you use exceptions (which is a good choice), don't hide them inside a catch. You'll only make your problem worse because the error is there, but the program will try to continue and will probably crash sometimes after, and corrupt anything it touches in the mean time.

paercebal
+5  A: 

I've developed on numerous multi-platform C++ apps (the largest being 1.5M lines of code and running on 7 platforms -- AIX, HP-UX PA-RISC, HP-UX Itanium, Solaris, Linux, Windows, OS X). You actually have two entirely different issues in your post.

  1. Instability. Your code is not stable. Fix it.

    • Use unit tests to find logic problems before they kill you.
    • Use debuggers to find out what's causing the crashes if it's not obvious.
    • Use boost and similar libraries. In particular, the pointer types will help you avoid memory leaks.
  2. Cross-platform coding.

    • Again, use libraries that are designed for this when possible. Particularly for any GUI bits.
    • Use standards (e.g. ANSI vs gcc/MSVC, POSIX threads vs Unix-specific thread models, etc) as much as possible, even if it requires a bit more work. Minimizing your platform specific code means less overall work, and fewer APIs to learn.
    • Isolate, isolate, isolate. Avoid in-line #ifdefs for different platforms as much as possible. Instead, stick platform specific code into its own header/source/class and use your build system and #includes to get the right code. This helps keep the code clean and readable.
    • Use the C99 integer types if at all possible instead of "long", "int", "short", etc -- otherwise it will bite you when you move from a 32-bit platform to a 64-bit one and longs suddenly change from 4 bytes to 8 bytes. And if that's ever written to the network/disk/etc then you'll run into incompatibility between platforms.

Personally, I'd stabilize the code first (without adding any more features) and then deal with the cross-platform issues, but that's up to you. Note that Visual Studio has an excellent debugger (the code base mentioned above was ported to Windows just for that reason).

Zathrus
A: 

After over 15 years of Windows development I recently wrote my first cross-platform C++ app (Windows/Linux). Here's how:

  • STL
  • Boost. In particular the filesystem and thread libraries.
  • A browser based UI. The app 'does' HTTP, with the UI consisting of XHTML/CSS/JavaScript (Ajax style). These resources are embedded in the server code and served to the browser when required.
  • Copious unit testing. Not quite TDD, but close. This actually changed the way I develop.

I used NetBeans C++ for the Linux build and had a full Linux port in no time at all.

Rob
A: 

Thanks everyone for your comments... I would love to vote up some of the answers but i dont have enough "reputation" :-)

I would also like to clarify that the code i was talking about is around 250k lines a it HAD lots of memory/initialization problems... 95% of them are fixed now. With hard work, use of c++ only, MS debugger and lots and lots of patience. We also use boost libraries all the time and that has help us lots. My recomendation is never ever say "I'LL WRITE MY OWN QUICKLY" haha.

I choosed the answer to the question just because the long int tip... that is going to save me a lot of time :-)

So by your replies... then the chrome idea sound like a bad one....

haha well in terms of bugs every little helps. Thanks everyone.

StfnoPad