1

I am trying to find a way to debug my application but it is very difficulty for me. The error is when I close the program, sometimes it shows the error code as below:

Unhandled exception at 0x7537812f in Sample.exe: 0xC0020001: The string binding is invalid

My application is a windows form written in C# in Visual studio 2012 Professional and the program uses some native functions from a dll which is written in C. I researched on the internet but almost all solutions are not using static variables or compile the dll with /clr in Visual Studio for C++, but my dll is C code from third party and it is built by MingGW so I can't follow these solutions. To change the static variables is impossible.

Please help me to find the solution for it?

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
user3085401
  • 11
  • 1
  • 2
  • Could you show the code where the exception is been thrown? And the signature of the external function? – Oscar Dec 16 '13 at 08:25
  • Your assumption that this is caused by C code cannot be correct, this is a C++/CLI exception. You **must** show an unmanaged call stack to make your question credible. Enable unmanaged debugging to get one. – Hans Passant Dec 16 '13 at 09:45

1 Answers1

0

Without code it's difficult to say what is the problem. But when you say, the crash happens (sometimes) during the application is closing, you probably can't provide the right piece of code easily.

The reasons could be, some code of the program uses some data, which is already destroyed. If the internal implementation of your DLL is C++ e.g. a destructor could access data already deleted or created statically before the instance of the class. This would be mean, the DLL causes the crash internally. For that explicite calling of some kind of close API function could be the only help.

Another cause is possibly, you instantiate the DLL in a C# class, which is destroyed in garbage collection. As the instances are destroyed "uncontrolled" they can call the already unloaded DLL API. A flag signaling the validity of DLL API would help for that cause.

So my suggestion are:

  • Check, If your DLL provides an API function for closing or cleaning up or unloading - call it and don't call any other API-function from DLL after that. Use e.g. a global flag tor that, and access DLL only if the flag allows it. (poor man's approach, better - ecapsulate the DLL in a C# class)

  • Try to load and unload DLL in any test-function of your C# for making the error easier reproducible.

  • Make sure, when your C# begins the closing process not to call any DLL function any more (except the once calling clean/close/unload)

Hope it helps.

Valentin H
  • 7,240
  • 12
  • 61
  • 111
  • I try to do following your guide but it is still not solved. I think that this exception realated to the using static variable in C program, but I can't remove all static variable in C source code. Do you have other solution ? – user3085401 Dec 19 '13 at 10:57