views:

1293

answers:

6

Hi

I'm getting really strange behavior in one of the DLLs of my C++ app. It works and loads fine until I include a single file using #include in the main file of the DLL. I then get this error message:

Loading components from D:/Targets/bin/MatrixWorkset.dll Could not load "D:/Targets/bin/MatrixWorkset.dll": Cannot load library MatrixWorkset: Invalid access to memory location.

Now I've searched and searched through the code and google and I can't figure out what is going on. Up till now everything was in a single DLL and I've decided to split it into two smaller ones. The file that causes the problems is part of the other second library (which loads fine).

Any ideas would really be appreciated. Thanks, Jaco

+2  A: 

Is it possible that header includes a #pragma comment(lib,"somelibrary.lib") statement somewhere? If so it's automatically trying to import a library.

To troubleshoot this I'd start by looking at the binary with depends (http://www.dependencywalker.com/), to see if there are any DLL dependencies you don't expect. If you do find something and you are in Visual Studio, you should turn on "Show Progress" AKA /VERBOSE on the linker.

Since you are getting the Invalid Access to memory location, it's possible there's something in the DLLMAIN or some static initializer that is crashing. Can you simplify the MatrixWorkset.dll (assuming you wrote it)?

kevin42
I'm pretty sure there isn't a #pragma in the included file since I wrote it myself and it works when included in a different DLL that also uses it.In dependency walker and everything looks good. The non-working DLL has a duplicate copy of the depending DLL which is not the case for the working DLL
Also, I've simplified MatrixWorkset.dll up to the point where it works when I don't include the header and then it works. I've compared this working copy to the non-working copy and the duplicate DLL is the only difference that I can see...
+4  A: 

The likely cause is a global with class type. The constructor is run from DllMain(), and DllMain() in turn runs before LoadLibrary() returns. There are quite a few restrictions on what you can do until DllMain() has returned.

MSalters
"There are quite a few restrictions on what you can do until DllMain() has returned." ... such as? Link please...
Gili
MSDN to the rescue: http://msdn.microsoft.com/en-us/library/ms682583.aspx
MSalters
A: 

The error you describe sounds like a run-time error. Is this error displayed automatically by windows or is it one that your program emits?

I say attach a debugger to your application and trace where this error is coming from. Is Windows failing to load a dependency? Is your library somehow failing on load-up?

If you want to rule in/out this header file you're including, try pre-compiling your main source file both with and without this #include and diff the two results.

veefu
A: 
A: 

I get that error from GetLastError() when I fail to load a DLL from a command line EXE recently. It used to work, then I added some MFC code to the DLL. Now all bets are off.

Tim
A: 

I just had this exact same problem. A dll that had been working just fine, suddenly stopped working. I was taking an access violation in the CRT stuff that initializes static objects. Doing a rebuild all did not fix the problem. But when I manually commented out all the statics, the linker complained about a corrupt file. Link again: Worked. Now I can LoadLibrary. Then, one by one, I added the statics back in. Each time, I recompiled and tested a LoadLibrary. Each time it worked fine. Eventually, all my statics were back, and things working normally.

If I had to guess, some intermediate file used by the linker was corrupted (I see the ilk files constantly getting corrupted by link.exe). If you can, maybe wipe out all your files and do a clean build? But I'm guessing you've already figured things out since this is 6 months old ...

Chris