让 Loki Singleton 在 VS 2008 C++ 中的 DLL 中工作

发布于 2024-07-24 01:24:10 字数 261 浏览 5 评论 0原文

我很确定这个问题并不新鲜,而且很难解决。 希望我对后者的看法是错误的。

我正在尝试在我的程序中使用来自 Modern C++ Design 的 Loki::Singleton 。

但是,我似乎无法让它跨 DLL 工作。 我想我知道为什么会发生这种情况:模板化代码在每个源模块中实例化,因此每个模块都有自己的全局变量,而不是一个全局变量。

显然,这使得 Singleton 变得非常非单一。

有什么办法可以绕过这种行为吗?

I'm pretty sure this problem isn't new, and pretty sure it's hard to solve. Hopefully I'm wrong about the latter.

I'm trying to use the Loki::Singleton from Modern C++ Design in a program of mine.

However, I can't seem to get it to work across DLLs. I think I know why this is happening: the templated code gets instantiated in every source module, so instead of there being one global variable, each module has its own.

Obviously, this makes the Singleton very much non-single.

Is there any way to get around this behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二手情话 2024-07-31 01:24:10

我在 Loki 源目录中看到它们有一个特定的 SingletonDLL 目录 正在测试中,看起来他们使用导出的、显式实例化的模板(可以工作)。 希望其中包含您想要的代码。

I see in the Loki source directory that they have a specific SingletonDLL directory under test, looks like they use an exported, explicitly instantiated template (which would work). Hopefully that contains the code you want.

暖阳 2024-07-31 01:24:10

请注意,这不会解决问题。 显式实例化和导出的单例应该可以解决问题...

-Rick

查看 #pragma data_seg 这里基本上,您需要在代码的共享部分中声明单例的实例。 默认情况下,静态数据的作用域为 dll。

使用模板可能会很棘手,但这是这里的成功之路,不涉及传递/复制静态数据。

Note this is not going to address the question. An explicitly instantiated and exported singleton should do the trick...

-Rick

Check out #pragma data_seg here basically, you need to declare an instance of the singleton in a shared section of your code. By default statics are scoped to the dll.

It may get tricky with templates, but this is the path to success here that doesn't involve passing / copying static data around.

以歌曲疗慰 2024-07-31 01:24:10

您可能是正确的,每个 DLL 都有自己的单例实例。 我不太熟悉 Loki 实现和 源代码 弄清楚并不是很有趣。

可能的解决方案是:

  • 不使用单例。 这实际上是我通常的偏好,因为您可以通过更改设计以不需要它们来避免整类问题。 有关为什么单例可能有害的长篇大论,请参阅这篇 Yegge 帖子。 我并不是那么强烈反对它们,但 95% 的情况下,单例会导致比它们解决的问题更多的问题(如果它们确实解决了任何问题的话)
  • 跨 DLL 边界复制静态成员。 我还以 hack 的方式完成了此操作,其中 DLL 从应用程序或另一个 DLL 获取指针,并将其自己的静态类成员副本重置为从外部传递的指针。 它很邪恶,很脏,你无法清理它,但它确实有效。

You are probably correct that each DLL has its own instance of the singleton. I'm not that familiar with the Loki implementation and the source code isn't a lot of fun to figure out.

Possible solutions are:

  • Not using singletons. This is actually my usual preference because you can avoid whole classes of issues by changing your design to not need them. For a long rant on why Singletons may be harmful see this Yegge post. I'm not THAT vehemently against them but 95% of the time Singletons cause many more problems than they solve (if they actually solve any)
  • Copying static members across DLL boundaries. I've also done this as a hack, where the DLL gets a pointer from an application or another DLL, and resets its own copy of the static class member to the pointer passed from outside. It's evil, it's dirty, you can't clean up after it, but it does work.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文