0

I have a .dll header which declares a class.

After the class declaration it instantiates a static object of the class.

The functions the .dll exports interface with the static object.

I'm getting an inexplicable segfault when the first call into one of these exported functions returns. So my question is: Is it OK to declare a static object in a .dll header like this:

class Foo{
public:
    void bar();
};

static Foo foo;

__declspec( dllexport ) void func() { foo.bar(); }
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • http://stackoverflow.com/questions/92546/variable-declarations-in-header-files-static-or-not – Piotr Siupa Aug 20 '15 at 22:27
  • @NO_NAME I don't understand the reason for the link. Can you help me? – Jonathan Mee Aug 20 '15 at 22:34
  • The link is about what is happen when you put definition of a variable to a header. In short: this is generally a bad idea. Perhaps [singleton](http://stackoverflow.com/questions/1008019/c-singleton-design-pattern) is better way. (It depends on what you want to achieve.) – Piotr Siupa Aug 20 '15 at 22:41
  • Write why you need variable `foo`, please. Knowing this, it will be easier to identify a solution. – Piotr Siupa Aug 20 '15 at 22:43
  • @NO_NAME Creating a `static Foo` avoids having to export the class definition, which I didn't want to do because the actual class contains a `vector`. I will only ever use one instance of the class, so I could make a singleton. Since I do not export the class that is effectively what I've done here, right? – Jonathan Mee Aug 20 '15 at 23:03
  • @JonathanMee: If you don't want people accessing `foo` directly, then it does not belong in the header file at all. Move it to a separate source file that implements `func()` and your other exported functions (which should not be implemented with inlined bodies in the header itself). – Remy Lebeau Aug 20 '15 at 23:07
  • I know too few about dlls to tell what is the best solution. Maybe it is enought to use [extern](http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c). – Piotr Siupa Aug 20 '15 at 23:08
  • @NO_NAME: No, the design of the header file is wrong to begin with, it needs to be re-written to get rid of `foo`. – Remy Lebeau Aug 20 '15 at 23:09

1 Answers1

1

For what you are attempting, you need to remove the class from the header file complete, it does not belong there at all.

Try something more like this:

MyDll.h (shared with projects that want to use your DLL):

#ifndef MyDllH

#ifdef BUILDING_DLL
#define MYDLL_EXPORT __declspec( dllexport )
#else
#define MYDLL_EXPORT __declspec( dllimport )
#endif

#ifdef __cplusplus
extern "C" {
#endif

MYDLL_EXPORT void func();
// other functions as needed...

#ifdef __cplusplus
}
#endif

#endif

MyDll.cpp: (compiled only as part of the DLL project):

#define BUILDING_DLL
#include "MyDll.h"

class Foo
{
public:
    void bar();
};

void Foo::bar()
{
    //...
}

static Foo foo;

void func()
{
    foo.bar();
}

// other functions as needed...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • This is clearly cleaner design (and I'm planning on swapping to this first thing) but it doesn't seem like it will have any impact on my arbitrary segfault. – Jonathan Mee Aug 21 '15 at 02:28