Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kuphryn

DLL & free() Heap Access Violation

This topic is 5739 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I am learning about dll. I created a simple dll with one class and one member function that returns a string object. The function looks something like this. ----- string MyClass::MyFunc() { string text = TEXT("Testing 1 2 3"); return text; } ----- In the main program, I statatically link the dll to the program. Everything compiles fine. However, the program crashes right after MyFunc(). ----- // Statically link DLL. #include "MyDLL.h" void MyWinClass::F1() { // Instantiate MyClass as local variable MyClass oneVar; // Attempt to get text from oneVar''s member function // MyFunc() should return a string object. It does. string newText = oneVar.MyFunc(); ----- Everything works up to and including the last line of code. I could even open the text MyFunc() returns. There is a big problem, however; the program crashes. This is the error Visual C++ shows when the program crashes after calling a function via the DLL. ----- HEAP[MyProgram.exe]: Invalid Address specified to RtlValidateHeap( 00360000, 00353FA0 ) Unhandled exception at 0x77f7f570 in MyProgram.exe.exe: User breakpoint. ----- In debug mode, the debugger shows a message box that mentioned something about "_BLOCK_TYPE_IS_INVALID(pHead->nBlockUsed)." Is there something wrong with the dll? The algorithm above works find if I call MyFunc() from inside another class. It seems that the program is trying to free something after return from the dll. Thanks, Kuphryn

Share this post


Link to post
Share on other sites
Advertisement
Trace into it, and find out exactly what deallocation is causing it. Might be something with the string allocators and the dll''s. Are you certain both your program and the dll were compiled using the same CRT?

Share this post


Link to post
Share on other sites
Magmai Kai Holmlor: Good point. I compiled the DLL in a separate project. Here is how I did it.

- project1 is DLL only
- project2 is EXE statically importing DLL

I thought it is okay to compile a dll separately and then using .dll, .lib, and .h files in any other project that will statically import the dll. Furthermore, you can use the .dll file in any project that dynamically imports the dll.

If the message I receive is what I believe it is, you need to compile all DLL and EXE in one project if and only if you want to statically import DLLs.

Thanks,
Kuphryn

Share this post


Link to post
Share on other sites
niyaw:

Thanks. I missed your message. Do I need to set the compiler for multithreaded DLL runtime in all projects that statically import DLL? Do I need to do the same for all DLL projects? How about projects that dynamically import DLLs?

Kuphryn

Share this post


Link to post
Share on other sites
niyaw:

Thanks. I missed your message. Do I need to set the compiler for multithreaded DLL runtime in all projects that statically import DLL? Do I need to do the same for all DLL projects? How about projects that dynamically import DLLs?

Kuphryn

Share this post


Link to post
Share on other sites
Okay. I believe I see the problem. The problem has to do with setting the compiler (Visual C++ .NET) to compile both the DLL project and EXE project (both debug and release versions) to "Multi-threaded DLL /MD."

The niyaw mentions is correct. Everything works, except for one problem. I am using MFC doc/view as program core. I have set the compiler to "Use MFC in a Static Library." I believe when you set to that option, the compiler automatically changes "Multi-threaded DLL /MD" to "Multi-threaded /MT."

So the bottomline is that the original problem has to do with multithreaded DLL setting and can be fixed. However, if I fix that problem, I cannot use MFC via Static Library. That is okay too, but then I would have to gather all MFC dll into one zip file before releases the program. Some people do not have MFC DLLs.

Kuphryn

Share this post


Link to post
Share on other sites
if you want to share data that is obtained from a c runtime between different modules, such as new''d data, stdio FILE pointers, strdup()ed strings, and so on, you must use the dll version of the runtime. i would stay away from statically linking to mfc in your case because you (with a great likelihood) may experience the same problems. mfc generally thinks that there is only one copy of it for the entire process. so i''d suggest dynamic linking for everything. finally, i believe you only need to distribute mfc42.dll with your project, but check dependency viewer to be on the safe side.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hey I made few standard dll and I''m worried someone might use it without my permission.
It is possible that someone could findout the parameters for my functions?
There is a way to stop people using the dll without my consent?

Thanks!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!