Re: [Boost-users] Boost Regex in a DLL

Dear Dr. John Maddock,
You can use Boost.Regex in a dll by defining BOOST_REGEX_DYN_LINK when building your dll, but as long as the regex code is isolated to your dll and doesn't spill over into it's public interface then this shouldn't be necessary.
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. Then I defined it in Properties->Preprocessor Definitions. Then it complains as: "Mixing a Boost dll with a static runtime is a really bad idea ...". I don't know why?
Other than that it's hard to say what the issue might be without seeing your code.
The surprising thing is that a single line of BOOST code (just an expression) disturbs the program. For example, I added: boost::regex sre("!NULL|sil|SIL"); only and didn't do anything with it. Then when I run the program with the perfectly working code (non-boost), it misbehaves. So I don't think it is a problem with my regex code as all. I don't think this problem is asscociated with the main program using my dll too. Otherwise, I must have missed something essential. I want to use Boost but I really need help at this point. Thank you in advance! mmoges _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

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.

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. Then I defined it in Properties->Preprocessor Definitions. Then it complains as: "Mixing a Boost dll with a static runtime is a really bad idea ...". I don't know why?
That's hint as to where the problem may be: it is in general a really bad idea to use the static C++ runtime library in a dll (unless you're *very* careful about what interfaces are exposed), and you cannot use regex as a dll with a static C++ runtime. The problem is that your application gets two different runtime libraries, which can lead to all kinds of hard to trace memory errors: this is a Win32 not a Boost issue.
Other than that it's hard to say what the issue might be without seeing your code.
The surprising thing is that a single line of BOOST code (just an expression) disturbs the program. For example, I added: boost::regex sre("!NULL|sil|SIL"); only and didn't do anything with it. Then when I run the program with the perfectly working code (non-boost), it misbehaves. So I don't think it is a problem with my regex code as all.
That sounds like a possible runtime-library problem. Can you switch both your application and the dll to use the dynamic C++ runtime? John.
participants (3)
-
Bronek Kozicki
-
John Maddock
-
Metasebia Moges