Neil
Neil

Reputation: 19

Stackoverflow error when I create a DLL exported object

first of all I apologize for any mistake, english is not my native language.

Here's my problem: I've written a DLL that exports a class derived from CSocket, everything works very fine except that the client, that implicit load the DLL, must instantiate the object as follows:

// Inside client constructor (client class scope object pointer)
CMyClass *m_lpMyObj = new MyClass;

but if the object is instantiated like this:

// Inside the client class header file (client class scope object)
CMyClass m_myObj;

when I run the client I get the error:

Unhandled exception at 0x775015de in MyApp.exe: 0xC00000FD: Stack overflow.

Same problem if inside a client method I write:

// Inside a client method (local object)
CMyClass myobj;

the only difference is that the error occurs (of course) when I invoke the method.

Here some details of the class:

// _AFXEXT defined for DLL project

#ifdef __cplusplus
extern "C" {
#endif

class CMyClass : public CSocket
{
  DECLARE_DYNCREATE(CModbusConnector)

public:
  AFX_EXT_CLASS CMyClass();  // Only some methods are exported
  AFX_EXT_CLASS virtual ~CMyClass();

  ....
};

#ifdef __cplusplus
}
#endif

The DLL is a CWinApp-derived object (regular DLL), I compiled the DLL as a Extension DLL with the same result.

Thanks in advance.

Regards,

Neil

Upvotes: 1

Views: 220

Answers (1)

MRAB
MRAB

Reputation: 20664

I think what's happening is that DECLARE_DYNCREATE is for an object whose size is calculated at runtime.

When you say:

CMyClass *m_lpMyObj = new MyClass;

it can, indeed, allocate the required memory for the instance, but when you say:

CMyClass m_myObj;

it doesn't have the true size at compile time, only the size of the 'stub', so when it tries to use the object it's writing beyond the end of the allocated space.

Upvotes: 1

Related Questions