1

I'm using ffmpeg in my C++ application.

When trying to play certain files an assertion inside of ffmpeg fails, which causes it to call abort() which terminates my application. I do not want this behavior, rather I want to get the chance to recover, preferably through an exception.

Anyone got any ideas as to how I can get around the problem with ffmpeg/assert potentially terminating my application?

EDIT:

The only way I can think of right now is to change the ffmpeg assert macro so that it causes an access violation which I can catch through SEH exceptions. Ugly and potentially bad solution?

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
ronag
  • 49,529
  • 25
  • 126
  • 221

3 Answers3

1

If the "exception" needs to be compiled as C, you could use a setjmp/longjump pair. Put the setjmp in your error handling code, and the longjmp in place of the abort in the FFMPG code.

If you really want a true exception to catch, a divide by zero might be safer than a random access violation.

AShelly
  • 34,686
  • 15
  • 91
  • 152
0

This code is from ffmpeg doxygen documentation

/*
 * copyright (c) 2010 Michael Niedermayer <michaelni@gmx.at>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

 #ifndef AVUTIL_AVASSERT_H
 #define AVUTIL_AVASSERT_H

 #include <stdlib.h>
 #include "avutil.h"
 #include "log.h"

 #define av_assert0(cond) do {                                           \
     if (!(cond)) {                                                      \
         av_log(NULL, AV_LOG_FATAL, "Assertion %s failed at %s:%d\n",    \
                AV_STRINGIFY(cond), __FILE__, __LINE__);                 \
         abort();                                                        \
     }                                                                   \
 } while (0)


 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 0
 #define av_assert1(cond) av_assert0(cond)
 #else
 #define av_assert1(cond) ((void)0)
 #endif


 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
 #define av_assert2(cond) av_assert0(cond)
 #else
 #define av_assert2(cond) ((void)0)
 #endif

 #endif /* AVUTIL_AVASSERT_H */

You could simply redefine the av_assert macros to throw instead of abort()

jrok
  • 54,456
  • 9
  • 109
  • 141
0

If you can't/don't want to re-work the ffmpeg code, then I'd say fork off another process to do the ffmpeg operations and then exit. You can wait for that process to exit one way or another in your main process and determine how it went, without risk of your main process being terminated.

It may not be the best solution in the world, but it gets you the isolation you need, with some hope of knowing what happened, and without having to do too much violence to the ffpmpeg code.

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79