11

I have a logging framework based on printf-style formatting:

void Logger::debug(const char *fmt, ...) {
    va_list args;
    va_start(args, fmt);
    this->output(DebugLevel, fmt, args);
    va_end(args);
}

If Logger::output throws, will the compiler unwind the stack properly, or do I need to add a try/catch block with va_end(args) in the catch clause? Can this be RAII'ed instead, or is va_end too magic for that? If possible, please include references to the standard.

3
  • "Can this be RAII'ed instead": no, va_* identifiers are macros. Jul 25, 2012 at 8:07
  • 4
    Really better if you don't use such things in C++. They are extremely unsafe and definitely unnecessary.
    – Puppy
    Jul 25, 2012 at 8:15
  • 1
    I think that wherever you have va_start() you will have to use va_end() inside the variadic function. Note that try-catch will significantly decrease performance if you plan on logging a lot.
    – Eitan T
    Jul 25, 2012 at 8:33

3 Answers 3

8

No, they cannot. The reasoning that they cannot because they are macros is silly. Macros are usable from constructors and destructors without any issues. However, va_start and va_end have specific requirements that they must be called from the same function. Moving them to separate functions is invalid. C++ refers to the C standard, and the C standard says "Each invocation of the va_start and va_copy macros shall be matched by a corresponding invocation of the va_end macro in the same function." (7.15.1) If you do call va_end from a helper class's destructor, it may work, or it may not. Since it doesn't meet the requirements of the standard, the behaviour is undefined.

Edit: as for the other question, do you need va_end at all when an exception is thrown, a legitimate argument could be made that "invocation of the va_end macro" doesn't actually require that the code reach the point where you invoke that macro (since macro invocation is strictly a compile-time-only action), but it strongly suggests that you do need it. So yes, use try/catch if exceptions are a possibility. The C99 rationale briefly notes in its description of va_copy that va_start may allocate memory. (I know of no implementation where it actually does do so.) On such an implementation, va_end would then deallocate that memory, so skipping va_end would cause a memory leak.

0

Yes, this can be RAII'ed using Boost.ScopeExit, even though va_start/va_end are macro's.

0

No va_start and va_end are macros. So, they cannot be RAII'ed. Also, no special care is needed for exceptions.

2
  • 4
    Special care is needed for exceptions. Returning from the function after invoking va_start without a matching va_end gives undefined behaviour. Jul 25, 2012 at 9:02
  • Undefined according to the standard, yes. But in >99% of actual implementations, va_end does nothing, and omitting it is harmless. You only need va_end if you care about the <1% of of super-obscure implementations for which it is actually necessary. Something undefined per the standard can still be defined per an implementation, and if it is defined the same in all the implementations you care about, in practice that is equivalent to the standard defining it. Aug 1 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.