Sign in to follow this  
XTC

stl & dll problem

Recommended Posts

I just started using VC 2003 1.xxx I allocate my object in main program and when I try calling by its adress in dll if function is using stl for example vector push_back() it is asserting. I previously used the same code for MSVC++ 6.0 without any problems. If I remove the stl part, it works. But with stl it is complaining. I hope i stated problem clearly enough. There's should be smt that i've missed

Share this post


Link to post
Share on other sites
well my main program is compiling without warnings. i have to ignore one default library libcmt.lib

I am using dynamically linked dll. After calling dll init func dll remembers pointer to my obj. so I should be able to call all functions from that dll without probs. but the problem is stl. Its internal alloc-dealloc when adding is what causing problems.

Share this post


Link to post
Share on other sites
With the static CRT, you cannot allocate memory in the DLL and free it in the EXE, or vice versa. std::vector::push_back() will probably perform an allocation (but not always, so it can be a hard bug to spot), and if memory is already allocated, it'll deallocate it too.
So if you add an element to the vector in the EXE, then tell the DLL to add an element, the DLL will free the memory allocated, then allocate new memory, and copy the old data into it, before pushing the new element. But because the memory was allocated by the EXE, and it's getting free'd in the DLL, you run into a problem.
The reason for this is that under Windows, the EXE and the DLL use different heaps, and new and delete aren't designed to handle multiple heaps. The solution is to use the DLL version of the CRT (so the calls to new and delete are both made from within the CRT DLL, not in the EXE or client DLL), or to use Linux (No, really, Linux doesn't have this problem [smile])

Share this post


Link to post
Share on other sites
Yeah, what Mr. Barber is saying is..

What version of the CRT did you use to link the .dll?

If you built the DLL out of static .libs that were built NOT using the .DLL versions, there will probably be conflicts

You'd have to build "DLL" versions of your static .libs to first link the DLL, and then non-DLL versions for your .exe code.

Share this post


Link to post
Share on other sites
umm... after doing a bit of research I think I finally understood the prob.
so now i can explain it to myself ... :D

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q172396

also, to avoid duplicates, you can use code generation-> use multithreaded dll, however, you should include crt dll's with your final exe

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this