0

I created a .dll (with MFC static linking and Windows Runtime libraries statically linked as well) and I am linking to a library which uses boost for memory management (the library is PCL). Everything compiles ok with no errors, but I noticed that inside the library code, memory allocation is not working properly. For intance, the following line

indices_.reset (new std::vector<int>);
try    {      
  indices_->resize (input_->points.size ());    
}

allocates a new std::vector, does not throw any exception, but the vector is still empty after the resize function. Why should this be?

If i allocate the vector myself inside my own code for the DLL, the allocation works properly. But other errors arrise, such as strings that suddenly disappear (and the Visual Studio debugger shows "Error reading memory" when I hover on those strings).

I use static linking of the library to the DLL (and I am using static Runtime libraries, /MT).

What could be happening?

2 Answers 2

1

The static runtime libraries is the problem.

You can only interoperate memory with the same runtime library. If you're static linking then the DLL and EXE have their own distinct copies- which is very bad if you're trying to pass complex types between them.

You need to dynamically link the runtimes on both sides to get one copy of the CRT if you want to share complex objects like std::vector.

I believe that MFC has the same issue as the CRT- you need to share a single copy, not have a copy each.

Sign up to request clarification or add additional context in comments.

2 Comments

This was indeed my suspicion. However, the OP didn't say that the library they link to statically also links to the CRT statically. I don't know if we can make this assumption (frankly I didn't think msvcrt even allowed static linking for a number years now)
But I have another question. Can I use CRT dynamically but still link MY libraries statically?
0

Q. Why should this be?

The most obvious explanation would be that points.size () returns 0.

Otherwise, there would be UB involved (like when linking conflicting versions of libraries or ODR violations)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.