简体   繁体   English

DLL中的单例被破坏

[英]Singleton in DLL being destroyed

I have C++ wrapped in C dll. 我用C ++包装了C ++。 The dll is called in my C# project. 在我的C#项目中调用dll。

In my wrapper functions I call a lot of Singletons, they are setup as follows: 在我的包装函数中,我调用了许多单例,它们的设置如下:

ComponentManager &ComponentManager::_cmpManager()
{
    static ComponentManager ONLY_ONE;
    return ONLY_ONE;
}

The above function is a static function inside my ComponentManager class. 上面的函数是我的ComponentManager类中的静态函数。 Here is the specific problem: 这是具体问题:

  bool createNewEntity(char *c)
{
    if (ComponentManager::_cmpManager().nameAvailable(c))
    {
        Entity e(c);
        Transform t;
        ComponentManager::_cmpManager().addComponent(c, t);
        SceneNode sc(CMP_MANAGER2.getComponent<Transform>(c));
        SCENE_MANAGER.addSceneNode(sc, e.entityName);
        return true;
    }
    return false;
}

Essentially what this does is the singleton has a Hash Map with a key type string, this function checks to see if this key already exists. 基本上它的作用是单例具有带有键类型字符串的哈希映射,该函数检查该键是否已存在。 The behaviour is always returning true. 行为总是返回true。 When I use a global object of type componentManager instead of the singleton it behaves correctly, so something is telling me the singleton keeps leaving scope and deleting itself. 当我使用类型为componentManager的全局对象而不是单例时,它的行为正确,所以有些东西告诉我单身人员不断离开范围并自行删除。 Also if I use the singleton in an application exe rather than a dll it behaves correctly. 此外,如果我在应用程序exe而不是dll中使用单例,它的行为正确。 So I have 2 questions, 所以我有两个问题,

  1. Is there a way to keep my singleton from traveling out of scope? 有没有办法让我的单身人士远离范围? If not. 如果不。
  2. Is there another way of setting up singletons to not be deleted after leaving scope? 是否有其他方法可以在离开范围后设置单身人士不被删除?

C++ static keyword is a bit different from C# static . C ++ static关键字与C# static略有不同。 See https://msdn.microsoft.com/en-us/library/y5f6w579.aspx for description. 有关说明,请参阅https://msdn.microsoft.com/en-us/library/y5f6w579.aspx

In item 2 there it says: 2. When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function. 在第2项中,它说: 2。当您在函数中声明变量时,static关键字指定变量在对该函数的调用之间保持其状态。

Try to declare your static not inside the method but in class scope (as per item 3 in above reference). 尝试声明你的静态不在方法内但在类范围内(按照上面参考中的第3项)。

3. When you declare a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. 3.在类声明中声明数据成员时,static关键字指定该类的所有实例共享该成员的一个副本。 A static data member must be defined at file scope. 必须在文件范围定义静态数据成员。 An integral data member that you declare as const static can have an initializer. 您声明为const static的整数数据成员可以具有初始化程序。

You will also need to declare that static member at file scope. 您还需要在文件范围内声明该静态成员。

Why it works when it is not in a dll -- it is not exactly clear. 当它不在dll中时为什么会起作用 - 它并不完全清楚。 Probably a peculiar behaviour of linker. 可能是链接器的特殊行为。 If class is declared in a dll it probably tries to instantiate class every time and your static inside the function is a new one every time. 如果在dll中声明了类,那么每次都可能尝试实例化类,并且每次函数中的静态都是新的。 But if class is inside the exe file it is somehow the same class every time and when you call your method _cmpManager() it always accesses the same instance of the class. 但是如果class在exe文件中,那么每次都是同一个类,当你调用方法_cmpManager()时,它总是访问该类的同一个实例。

Just my two cents :-). 只是我的两分钱:-)。

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

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