|
Boost Users : |
From: Bronek Kozicki (brok_at_[hidden])
Date: 2007-04-20 14:05:28
Metasebia Moges wrote:
> Like I said in an earlier e-mail, the code is isolated. However, to make
> things sure I added it as:
> #define BOOST_REGEX_DYN_LINK in the source code and there is no change.
wrong.
> Then I defined it in Properties->Preprocessor Definitions. Then it
correct, this is how macros like this should be defined. BTW, I see you
are using Visual C++ - it is relevant for the rest of this email. If I'm
mistaken, just ignore it.
> complains as:
> "Mixing a Boost dll with a static runtime is a really bad idea ...". I
> don't know why?
because it really is bad idea. I will explain why: C++ runtime needs
access to the heap in order to be able to create objects (like strings).
On Windows each process owns multiple heaps and the runtime holds handle
to one. If you use static runtime, this handle is linked into executable
(it's a static variable). On Windows you have two kinds of executables:
DLL and EXE. Each copy of static C++ runtime that is statically linked
into an executable will have own heap handle, thus heaps (of EXE and all
DLLs linked with static runtime, running inside the process) will be
separate. Now imagine what happens if function inside a DLL creates a
string (be it 'std::string("foo bar")' or simply 'new char[20]') and
returns it to EXE that called it - string would be created in the
context of C++ runtime statically linked with the DLL (one heap) and
then passed (and some time later destroyed) in the context of another
C++ runtime, the one statically linked into the EXE (other heap). The
trouble is that you cannot allocate memory on one heap and then free it
on another, because they know nothing of one another - the attempt will
likely simply corrupt the heap of the other runtime (thus crashing your
process some time later). This is why whenever you want to use DLL and
EXE and pass C++ objects of or pointers between them, it is crucial that
they all (DLLs and EXE) use one copy of the C++ runtime - that is
dynamically linked one (option /MD for release build and /MDd for debug).
I hope this answers your questions
B.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net