c# - Prevent graceless crash from occurring in application calling library via P/Invoke -
c# - Prevent graceless crash from occurring in application calling library via P/Invoke -
i have c# application app a
calls, via p/invoke, c++ (qt) dll app b
. let's assume cannot, means, edit app b
, know app b
throwing out of memory exception
under set of conditions can replicate not accurately test before passing input app a
app b
. exception not pose threat app a
, ignored if app b
"reset" in manner, when app b
crashes calls abort()
in turn causes app a
's runtime terminate.
how can prevent app b
's inevitable, unpredictable, , mundane crash impacting app a
?
notes:
an implementedunhandledexceptionhandler
ignored when error thrown. app b
crashes due bug in qtwebkit feasibly handled app b
via deleting oversize object , returning null. app a
not study out of memory , machine has more plenty memory perform app b
's operation several times over, regardless of bug, memory apparently not allocated app b
whatever reason.
you can handle sigabrt coming unmanaged code, it's little messy. it's possible done in c#, method requires c++/cli assembly create work (at to the lowest degree in 1 process).
step 1: create c++/cli class library project, in *.h file set next function
namespace classlibrary1 { public ref class class1 { public: static void callaborter(); }; }
step 2: in cpp file set next code:
// main dll file. #include "stdafx.h" #include <csetjmp> #include <csignal> #include <cstdlib> #include <iostream> #include "classlibrary1.h" #include "..\win32project1\win32project1.h" #pragma managed(push, off) jmp_buf env; void on_sigabrt (int signum) { longjmp (env, 1); } void run() { if (setjmp (env) == 0) { signal(sigabrt, &on_sigabrt); fnwin32project1(); } else { std::cout << "aborted\n"; } } #pragma managed(pop) void classlibrary1::class1::callaborter() { run(); }
i left names of classlibrary , class default, , aborting function phone call fnwinproject1(). need link offending dll straight project (just it's in c++)
in c# class, include reference c++/cli assembly , phone call "callaborter" method.
the code in run setup catcher sigabrt (called abort()) , process in on_sigabrt(), cause whole function bail. can clean , seek again.
i leave exercise op take useful names.
note: in debug still abort dialog, press ignore continue. in release not see dialog.
p.s. should mention built response based on stackoverflow question: how handle sigabrt signal?
c# c++
Comments
Post a Comment