Passing reference to STL vector over dll boundary

前端 未结 7 1865
一个人的身影
一个人的身影 2020-12-01 08:02

I have a nice library for managing files that needs to return specific lists of strings. Since the only code I\'m ever going to use it with is going to be C++ (and Java but

7条回答
  •  不思量自难忘°
    2020-12-01 09:00

    The problem occurs because dynamic (shared) libraries in MS languages use a different heap than the main executable. Creating a string in the DLL or updating the vector that causes a reallocation will cause this issue.

    The simplest fix for THIS issue is to change the library to a static lib (not certain how one makes CMAKE do that) because then all the allocations will occur in the executable and on a single heap. Of course then you have all of the static library compatibility issues of MS C++ which make your library less attractive.

    The requirements at the top of John Bandela's response are all similar to those for the static library implementation.

    Another solution is to implement the interface in the header (thereby compiled in the application space) and have those methods call pure functions with a C interface provided in the DLL.

提交回复
热议问题