I recently decided to export some of my functions to a static library (.lib). I also decided not to give the complete header files to the Users. I kept private and protected variables and methods out of it, as the end-users should not use them and should not have to include other headers for Class declarations.
Unexpectedly I ran into heap-corruption errors when creating instances of the Classes which I exported to the static lib.
This is my original header file:
// Original Header File
class CODBCHelper
{
public:
CODBCHelper();
virtual ~CODBCHelper();
public:
std::vector<UserData> getUserInformation();
private:
SQLHENV m_henv;
SQLHDBC m_hdbc;
SQLHSTMT m_hstmt;
};
Then I decided to take the private variables out, so that users of my .lib do not abuse them and do not have to include unnecessary SQL Header files:
// Minimized Header File
class CODBCHelper
{
public:
CODBCHelper();
virtual ~CODBCHelper();
public:
std::vector<UserData> getUserInformation();
};
I noticed that the sizeof
Operation returns different values depending from where it is called.
Called in the .lib:
int size = sizeof(CODBCHelper);
// size is 12
Called in the linking project:
int size = sizeof(CODBCHelper);
// size is 1
Doing the following code in my linking project then causes a heap corruption (which happens probably because of wrong size calculations):
CODBCHelper* helper = new CODBCHelper;
Doing something like
class CODBCHelper
{
[...]
private:
char padding[12];
}
would solve the problem, but i think its really bad behavior and not maintainable at all.
So is there any way to tell the compiler how big the real Object (from the linked library) is going to be? Or is there any easy way of hiding declarations in header files that are not needed by users?