f



Question about mixing c++ exception handling & c code

Hi,

I was looking thru code in my organisation. same dll is sharingcode is
mixing c++ code & c code, which is being again called by some other c+
+ dll.

Only issue I am not too sure about exception handling in this case -

here is trimmed code snippet -

include.h
typedef void (func*)(void *,bool);
struct scriptparams
{
func callback;
void *obj;
};

test.cpp
class execption
{
public:
virtual void printerror(){//do something};
};
class foo
{
 public:
foo(){};
void dosomething(bool arg)
{
  cout>>"do something">>endl;
  if(!arg) throw execption;
  //if valid do something
};
};
extern "C" void dosomethingwrapper(void *obj_,bool arg)
{
foo *fooptr = reinterpret_cast<foo*>obj_;
fooptr->dosomething(arg);
};

test1.c
#include <include.h>
void
executescript(scriptparams* params)
{
 bool ifValid = false;
 //calculate something to get value of
 params->callback(params->obj,ifValid);
}

test2.cpp
#include <include.h>
class scriptexecuter
{
public:
scriptexecuter(char* arg1,char* arg2)
{// get these args & do someprocessing};
void execute()
{
scriptparams params;
params->callback = dosomethingwrapper;
params->obj = this;
try{
executescript(params);
}
catch(execption &e)
{
e.printerror();
}
};
};

test.cpp & test1.c are being compiled in one dll lets call dll1 & othe
being compiled into another dll -> dll2.

I am not sure what to expect if foo::dosomething raises an exception.
Is code safe for exception being traversing through c code.
Can I assume object which is being caught in scriptexecuter::execute
to be properly constructed one.

Thank you for your advice.

Regards,
Abhijeet
0
11/24/2008 7:07:19 PM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

6 Replies
370 Views

Similar Articles

[PageSpeed] 41

abhijeet <abhi.10dulkar@gmail.com> kirjutas:

> Hi,
> 
> I was looking thru code in my organisation. same dll is sharingcode is
> mixing c++ code & c code, which is being again called by some other c+
> + dll.
> 
> Only issue I am not too sure about exception handling in this case -

Passing C++ exceptions through C code is undefined behavior. You can 
easily got a segfault or something worse.

The implementation may define the behavior, though. Most notably, MSVC++ 
does, as C++ exceptions are implemented on top of Windows OS-level 
"structured exceptions" which are also supported in C. However, to handle 
them properly (like releasing any held resources during stack backwind), 
the C code must use non-standard extensions for exception handling by 
itself, and I'm by some reason quite sure it often doesn't. I.e. you can 
use this technique only in the case when the C interface explicitly 
documents that it supports C++ exceptions (that is, on this specific 
platform).

For handling C++ exceptions portably, any C++ function which might be 
called from C code should catch all exceptions and not let thenm 
propagate to the C code. For example, it can convert them into error 
codes or log them somewhere else.

hth
Paavo
0
paavo9668 (26)
11/24/2008 8:05:36 PM
On Nov 24, 11:07=A0am, abhijeet <abhi.10dul...@gmail.com> wrote:
> Hi,
>
> I was looking thru code in my organisation. same dll is sharingcode is
> mixing c++ code & c code, which is being again called by some other c+
> + dll.


There is no need to translate any code with a c-compiler.
Ansi C code compiles with a C++-compiler.
My advice is to get rid of C code in favor or exception-safe-C++ code.
0
11/24/2008 10:22:14 PM
On Nov 24, 11:07=A0am, abhijeet <abhi.10dul...@gmail.com> wrote:
> I am not sure what to expect if foo::dosomething raises an exception.
> Is code safe for exception being traversing through c code.


As long as the C-code is being translated using a C++ compiler you're
fine.

0
11/24/2008 10:29:19 PM
Peter wrote:
> On Nov 24, 11:07 am, abhijeet <abhi.10dul...@gmail.com> wrote:
>> Hi,
>>
>> I was looking thru code in my organisation. same dll is sharingcode is
>> mixing c++ code & c code, which is being again called by some other c+
>> + dll.
> 
> 
> There is no need to translate any code with a c-compiler.
> Ansi C code compiles with a C++-compiler.

No, it does not.  There's plenty of legal C that isn't legal C++.

-- 
Ian Collins
0
ian-news (10155)
11/25/2008 6:38:42 AM
Peter wrote:
> On Nov 24, 11:07 am, abhijeet <abhi.10dul...@gmail.com> wrote:
>> I am not sure what to expect if foo::dosomething raises an exception.
>> Is code safe for exception being traversing through c code.
> 
> 
> As long as the C-code is being translated using a C++ compiler you're
> fine.
> 
In that case, it's C++ code.

-- 
Ian Collins
0
ian-news (10155)
11/25/2008 6:39:05 AM
On 24 Nov., 23:22, Peter <ExcessPh...@gmail.com> wrote:
> On Nov 24, 11:07=A0am, abhijeet <abhi.10dul...@gmail.com> wrote:
>
> > Hi,
>
> > I was looking thru code in my organisation. same dll is sharingcode is
> > mixing c++ code & c code, which is being again called by some other c+
> > + dll.
>
> There is no need to translate any code with a c-compiler.
> Ansi C code compiles with a C++-compiler.
> My advice is to get rid of C code in favor or exception-safe-C++ code.

While I agree with you that most valid C code very likely also either
is valid C++ code or trivially can be changed into legal C++ code, it
is not enough to also make it "real" C++ code. Most important in this
aspect is the lack of exception safety, and this alone should be
reason enough to refrain you from taking such a short-cut.

/Peter
0
11/25/2008 6:56:10 PM
Reply: