繁体   English   中英

为什么静态库中的模板化函数在链接到动态库时会崩溃?

[英]Why are templated functions in a static library crashing when linked to a dynamic library?

在使用 XCode 11.6 的 OSX 上,我将 v8 构建为静态库 (libv8_monolith.a)。 在一种情况下,我将它链接到一个 Executable 并且一切正常,在另一种情况下我将它链接到一个 Bundle(动态库)并且模板化函数崩溃 EXC_BAD_ACCESS:

XCode 崩溃调用堆栈

例如AllocatePage()

内存分配器.h

template <MemoryAllocator::AllocationMode alloc_mode = kRegular, typename SpaceType> EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
Page* AllocatePage(size_t size, SpaceType* owner, Executability executable);

extern template EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
Page* MemoryAllocator::AllocatePage<MemoryAllocator::kRegular, PagedSpace>(size_t size, PagedSpace* owner, Executability executable);

内存分配器.cc

template <MemoryAllocator::AllocationMode alloc_mode, typename SpaceType>
Page* MemoryAllocator::AllocatePage(size_t size, SpaceType* owner, Executability executable) {
  MemoryChunk* chunk = nullptr;
  if (alloc_mode == kPooled) {
    DCHECK_EQ(size, static_cast<size_t>(
                        MemoryChunkLayout::AllocatableMemoryInMemoryChunk(
                            owner->identity())));
    DCHECK_EQ(executable, NOT_EXECUTABLE);
    chunk = AllocatePagePooled(owner);
  }
  if (chunk == nullptr) {
    chunk = AllocateChunk(size, size, executable, owner);
  }
  if (chunk == nullptr) return nullptr;
  return owner->InitializePage(chunk);
}

template EXPORT_TEMPLATE_DEFINE(V8_EXPORT_PRIVATE)
Page* MemoryAllocator::AllocatePage<MemoryAllocator::kRegular, PagedSpace>(size_t size, PagedSpace* owner, Executability executable);

如果我改为将其编写为非模板化版本,并重新编译 libv8_monolith.a,则不会出现崩溃:

Page* AllocatePage2(size_t size, PagedSpace* owner, Executability executable);

Page* MemoryAllocator::AllocatePage2(size_t size, PagedSpace* owner, Executability executable) {
  MemoryChunk* chunk = nullptr;
  if (chunk == nullptr) {
    chunk = AllocateChunk(size, size, executable, owner);
  }
  if (chunk == nullptr) return nullptr;
  return owner->InitializePage(chunk);
}

请注意,这些崩溃的模板化函数都没有暴露在外部(它们不是 v8.h API 的一部分),它们都是 v8 内部“命名空间内部”的代码。

是否有一些我遗漏的编译器或链接器标志? 这甚至是我应该做的事情吗? 我是否必须将 v8 编译为动态库才能在另一个动态库中使用它?

事实证明 After Effects 内部有自己的 v8 动态库,因此当 After Effects 加载我们的动态库时,某些函数之间会发生名称冲突。 我现在的解决方案是使用更改的命名空间重新编译 v8 以避免冲突。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM