DLL Access Violation When Newing C++ Class From C Function

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This is my first foray into windows DLLs and Ive been converting a working *nix shared object library over to microsoft land. Im using the interface plugin paradigm and havent had any problems on the *nix systems but am geting AVs when compiling/running
in Vista.
If i make a direct function call of the one of the export functions, everything works fine. But it fails in a Thread with Access violation - code c0000005 when I try to create an instance of the plugin object that is internal to the dll.
My functor is defined as:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; typedef Module* create_t();
[/code]
And the Loading of the DLL looks like:
<div style="color:Black;background-color:White; <pre>
#<span style="color:Blue; if defined _WIN32 || defined _WIN64
::SetLastError(0);
<span style="color:Blue; void* libHandle = LoadLibraryA(libfile.c_str());
#<span style="color:Blue; else
<span style="color:Blue; void* libHandle = dlopen(libfile.c_str(), RTLD_LOCAL | RTLD_LAZY);
#endif
[/code]
And to Construct the Module I get the create function from the dll as such:
<div style="color:Black;background-color:White; <pre>
#<span style="color:Blue; if defined _WIN32 || defined _WIN64
create_t* create_module = (create_t*) (GetProcAddress((HMODULE)libHandle, <span style="color:#A31515; "Create"));
#<span style="color:Blue; else
create_t* create_module = (create_t*) dlsym(libHandle, <span style="color:#A31515; "Create");
#endif
[/code]
The invocation of the Create function works as expected:
<div style="color:Black;background-color:White; <pre>
Module* libModule = create_module();
[/code]
As the stack ends up putting me inside the exported DLL function:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; extern <span style="color:#A31515; "C"
{
EXPORT Module* Create()
{
<span style="color:Blue; return <span style="color:Blue; new Logger();
}
}
[/code]
However it crashes on the new Logger() call just before it gets into the constructor:
<div style="color:Black;background-color:White; <pre>
Logger::Logger()
{
}
[/code]
Im rather rusty on my assembly so Im going to include that below incase it helps (it didnt for me) The last line of the assembly is the line that triggers the AV:
<div style="color:Black;background-color:White; <pre>
5F131790 push ebp
5F131791 mov ebp,esp
5F131793 push 0FFFFFFFFh
5F131795 push offset __ehhandler$??0Logger@@QAE@XZ (5F141F3Eh)
5F13179A mov eax,dword ptr fs:[00000000h]
5F1317A0 push eax
5F1317A1 sub esp,130h
5F1317A7 push ebx
5F1317A8 push esi
5F1317A9 push edi
5F1317AA push ecx
5F1317AB lea edi,[ebp-13Ch]
5F1317B1 mov ecx,4Ch
5F1317B6 mov eax,0CCCCCCCCh
5F1317BB rep stos dword ptr es:[edi]
5F1317BD pop ecx
5F1317BE mov eax,dword ptr [___security_cookie (5F14A540h)]
5F1317C3 xor eax,ebp
5F1317C5 push eax
5F1317C6 lea eax,[ebp-0Ch]
5F1317C9 mov dword ptr fs:[00000000h],eax
5F1317CF mov dword ptr [ebp-14h],ecx
5F1317D2 mov ecx,dword ptr [ebp-14h]
5F1317D5 call 5F130000
5F1317DA mov dword ptr [ebp-4],0
[/code]
Btw EXPORT is defined as:
<div style="color:Black;background-color:White; <pre>
#define EXPORT __declspec(<span style="color:Blue; dllexport)
[/code]


Im sure there is something foolish Im doing but for the life of me Ive cant seem to figure out the issue. Both the executable and dll are compiled with the same compiler on the same system. I can provide the configuration information
if that would be useful.
But Im a bit tired of seeing:
<div style="color:Black;background-color:White; <pre>
First-chance exception at 0x5f130000 <span style="color:Blue; in Test3D.exe: 0xC0000005: Access violation.
Unhandled exception at 0x5f130000 <span style="color:Blue; in Test3D.exe: 0xC0000005: Access violation.
[/code]
Here is the stack from VS2010 Express:
<div style="color:Black;background-color:White; <pre>
Logger.dll!5f130000()
> Logger.dll!Logger::Logger() Line 78 + 0x4a bytes C++
Logger.dll!Create() Line 23 + 0x24 bytes C++
Test3D.exe!ModuleManager::Load(std::basic_string<<span style="color:Blue; char,std::char_traits<<span style="color:Blue; char>,std::allocator<<span style="color:Blue; char> > libfile) Line 137 + 0x5 bytes C++
Test3D.exe!ThreadMain(<span style="color:Blue; void * threadData) Line 57 C++
Test3D.exe!boost::_bi::list1<boost::_bi::<span style="color:Blue; value<<span style="color:Blue; void *> >::<span style="color:Blue; operator()<<span style="color:Blue; void * (__cdecl*)(<span style="color:Blue; void *),boost::_bi::list0>(boost::_bi::type<<span style="color:Blue; void> __formal, <span style="color:Blue; void * (<span style="color:Blue; void *)* & f, boost::_bi::list0 & a, boost::_bi::type<<span style="color:Blue; void> __formal) Line 253 + 0x23 bytes C++
Test3D.exe!boost::_bi::bind_t<<span style="color:Blue; void,<span style="color:Blue; void * (__cdecl*)(<span style="color:Blue; void *),boost::_bi::list1<boost::_bi::<span style="color:Blue; value<<span style="color:Blue; void *> > >::<span style="color:Blue; operator()() Line 21 C++
Test3D.exe!boost::detail::thread_data<boost::_bi::bind_t<<span style="color:Blue; void,<span style="color:Blue; void * (__cdecl*)(<span style="color:Blue; void *),boost::_bi::list1<boost::_bi::<span style="color:Blue; value<<span style="color:Blue; void *> > > >::run() Line 62 C++
Test3D.exe!boost::`anonymous <span style="color:Blue; namespace::thread_start_function(<span style="color:Blue; void * param) Line 172 C++
msvcr100d.dll!_callthreadstartex() Line 314 + 0xf bytes C
msvcr100d.dll!_threadstartex(<span style="color:Blue; void * ptd) Line 297 C
kernel32.dll!755beccb()
[Frames below may be incorrect and/or missing, no symbols loaded <span style="color:Blue; for kernel32.dll]
ntdll.dll!76f2d80d()
ntdll.dll!76f2da1f()
[/code]

Any insight to what Im overlooking would be huge since google has turned up nothing except a similar question asked 4 years ago but never answered in these forums.
Thanks!

View the full article
 
Back
Top