boost::function and cross dll call issue

Hello, I have a weird access violation when using boost::function which is passed from one DLL to another. At work we use an engine that loads libraries at runtime. One of those libraries provides a class, that looks like that: class CLib { public: LIB_API CLib(void); typedef boost::function<void ()> Func; LIB_API void virtual Invoke(Func * func); }; Another library tries to make use of this lib by void freedummy() { int x = 0; } CLib::Func f = freedummy; f(); // testinvoke locally c.Invoke(&f); // passing to dll The invocation fails hard with Visual Studio 2008 SP1 (Windows 7 64bit, application as 32 bit) when passing to the other dll. The result is a call to a function address that points to nomans-land (c.Invoke()), whereas the direct f() call invokes the freedummy correctly. I have tried to create a very basic reproducer for this case and I could not make it crash. I tried to track down the execution of freedummy in the disassembler. The f-Pointer remains the same all the time but when boost::function0<void>::operator()() gets called on the lib the ptr to f remains correct but the inner function_ptr pointing to freedummy gets corrupted and pointing into garbage. Since I could NOT reproduce it with a simple test setup and I actually am not very sure why this fails I am a bit out of ideas. Can there be somehow a misalignment or different assembler (The dlls may have been compiled with different settings, not sure). I just remembered that this line: vtable_type* get_vtable() const { return reinterpret_cast<vtable_type*>( reinterpret_cast<std::size_t>(vtable) & ~static_cast<size_t>(0x01)); } (in function_template.hpp on 678) looked different in the disassembler when called f() in the main executable compared to the invocation in CLib in the external dll. Can I somehow get a better grip on what's happening? My assembler is not that strong that I could understand all those calls when journeying down the boost call stack. Any hint appreciated, Sam -- Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

----- Original Message -----
From: "ariasgore@gmx.de" <ariasgore@gmx.de> To: boost-users@lists.boost.org Cc: Sent: Friday, May 4, 2012 8:55 PM Subject: [Boost-users] boost::function and cross dll call issue
Hello, I have a weird access violation when using boost::function which is passed from one DLL to another. At work we use an engine that loads libraries at runtime.
You MUST check following basic points: 1. Make sure that they use **exactly same** version of Boost with same flags (debug/release etc) 2. Do all the DLLs use exactly the same runtime, such that something allocated in one DLL and deallocated in other calls exactly the same functions. Best try to check it with depends.exe program and check if the use and load same MSVCxxx.DLL Including same debug/release and exact version. 2. Make sure that they both use Dynamic Runtime linking (i.e. do not link with MSVC runtime statically) Just hints to debug Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/

Hello, some of those points can be confirmed since I integrated boost into our environment: For 1: We do only have one boost version and both binaries do not use anything compiled from boost. No special flags have been set for the pre processor. For 2: We do never mix debug runtime libraries and all libs do use the /MDd flag so that the dlls share the same runtime context. But I will check that with dependency walker again. Both binaries are build on the same machine by the same compiler. However the noted fact that the assembler code is different somehow bothers me since I cannot confirm that the binaries itself are build with the same compiler settings. Anyway since I am in debug I do expect no changes there since we usually build debug without any optimization. Thanks Sam
From: "ariasgore@gmx.de" <ariasgore@gmx.de> To: boost-users@lists.boost.org Cc: Sent: Friday, May 4, 2012 8:55 PM Subject: [Boost-users] boost::function and cross dll call issue
Hello, I have a weird access violation when using boost::function which is
----- Original Message ----- passed from
one DLL to another. At work we use an engine that loads libraries at runtime.
You MUST check following basic points:
1. Make sure that they use **exactly same** version of Boost with same flags (debug/release etc)
2. Do all the DLLs use exactly the same runtime, such that something allocated in one DLL and deallocated in other calls exactly the same functions.
Best try to check it with depends.exe program and check if the use and load same MSVCxxx.DLL
Including same debug/release and exact version.
2. Make sure that they both use Dynamic Runtime linking (i.e. do not link with MSVC runtime statically)
Just hints to debug
Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone! Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a

Sooo, I found the bogus. The host application had a struct member alignment set to 16 bytes which did a bad job for the boost header :) Sam -------- Original-Nachricht --------
Datum: Sat, 5 May 2012 09:43:51 -0700 (PDT) Von: Artyom Beilis <artyomtnk@yahoo.com> An: "boost-users@lists.boost.org" <boost-users@lists.boost.org> Betreff: Re: [Boost-users] boost::function and cross dll call issue
From: "ariasgore@gmx.de" <ariasgore@gmx.de> To: boost-users@lists.boost.org Cc: Sent: Friday, May 4, 2012 8:55 PM Subject: [Boost-users] boost::function and cross dll call issue
Hello, I have a weird access violation when using boost::function which is
----- Original Message ----- passed from
one DLL to another. At work we use an engine that loads libraries at runtime.
You MUST check following basic points:
1. Make sure that they use **exactly same** version of Boost with same flags (debug/release etc)
2. Do all the DLLs use exactly the same runtime, such that something allocated in one DLL and deallocated in other calls exactly the same functions.
Best try to check it with depends.exe program and check if the use and load same MSVCxxx.DLL
Including same debug/release and exact version.
2. Make sure that they both use Dynamic Runtime linking (i.e. do not link with MSVC runtime statically)
Just hints to debug
Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
participants (2)
-
ariasgore@gmx.de
-
Artyom Beilis