Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi.

I have a strange problem with P/Invoke: every PInvoke call produces a StackOverflowException in mscorlib.dll. Even LoadLibrary or GetProcAddress. The strangest thing is that the code is running fine, I get the right return value back and so on. But the Exception is thrown a short time afer the call... The same code part checked out in the old revision is running fine. It seem's to be a settings problem in the Project/Solution, but I cannot find any thing... Any Ideas? Something with premissions / signing? If I comment out the P/Invoke call and replace it with a constant, no exception occures...

Thanks a lot!
Andreas
Posted
Comments
Keith Barrow 15-Aug-11 16:25pm    
Odd. Can you step through your code? You can also tell Visual Studio to break on all exceptions, this might stop in the problem code. More than likely you've got code infinitely recursing somewhere.
The other thing to do is to restart your machine, sometimes VS and/or the OS gets its knickers in a twist (I think that's the technical term!) and kicking them sorts odd problems out.
andreas1024 15-Aug-11 16:42pm    
thank you for quick reply. If I step through the dll calls everything is fine, the result is fine, the call happens only once (no infinity recursing) but the exception is thrown later in mscorlib.dll or (sometimes) in windowsbase.dll where I can't step / break.
I've checked out an old revision as a separate copy and if I start the old one everything goes well. Even if I replace the main application with the new one (the calls are made in a class library) everything is fine. But with the new compiled version of the library... nothing...
Sergey Alexandrovich Kryukov 15-Aug-11 21:25pm    
It should be an infinite recursion. How do you know it is not? First, catch all exceptions on top of stack of each thread, catch the exception and provide its full dump. Indicate the line numbers of the code where exception was thrown and propagated. Show the relevant parts of code and indicate those line numbers.
--SA
andreas1024 16-Aug-11 0:16am    
I have a method like this:
public static bool Init()
{ IntPtr dll = LoadLibrary("some.dll");
return true; }
after the method is called, the StackOverflowException is thrown in mscorlib.dll a few calls after (not right there! if I break there and step through I return to the constructor of the window)
If I replace the method with
public static bool Init()
{ return true; } no exception is thrown. So if there is a infinite recursion, it must be in LoadLibrary as I see... In fact, the original method was longer, after the LoadLibrary call I call GetProcAddress, GetDelegateForFunctrionPointer and call the delegate, but even if I return just after the LoadLibrary (like here), the stack is corrupted.
I'll provide a stack dump in about 1 hour from office PC, hope it helps. Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900