Struct with Vector<BYTE> size different in VS2013
-
I have the following structure defined inside my DLL and a COM component we developed. struct ContentStr { ContentStr():ptr(NULL), contSize(0), structSize(0), X(0), Y(0){} const BYTE* ptr; int contSize; int structSize; //output std::vector m_v_Data; int X; int Y; private: ContentStr ( const ContentStr & rhs ); ContentStr & operator= ( const ContentStr & rhs ); }; The COM component invokes a DllExport method in the DLL which accepts the structure as reference and fills its value. It was working fine in VS2008. Recently I ported my code base to VS2013. Now the method throws exception when invoked. When I checked the size of the structure using sizeof operator, I was surprised a bit. VS 2008: /*Inside COM*/ sizeof(ContentStr) = 40 /*Inside DLL*/ sizeof(ContentStr) = 40 VS 2013: /*Inside COM*/ sizeof(ContentStr) = 36 /*Inside DLL*/ sizeof(ContentStr) = 32 I could see that the difference in size is due to the difference in vector size. Searching the internet gave me huge confusion hence I am posting this question on the expert forum. Could someone please explain me why is this difference seen in the sizes and help me find a solution.
Kings
-
I have the following structure defined inside my DLL and a COM component we developed. struct ContentStr { ContentStr():ptr(NULL), contSize(0), structSize(0), X(0), Y(0){} const BYTE* ptr; int contSize; int structSize; //output std::vector m_v_Data; int X; int Y; private: ContentStr ( const ContentStr & rhs ); ContentStr & operator= ( const ContentStr & rhs ); }; The COM component invokes a DllExport method in the DLL which accepts the structure as reference and fills its value. It was working fine in VS2008. Recently I ported my code base to VS2013. Now the method throws exception when invoked. When I checked the size of the structure using sizeof operator, I was surprised a bit. VS 2008: /*Inside COM*/ sizeof(ContentStr) = 40 /*Inside DLL*/ sizeof(ContentStr) = 40 VS 2013: /*Inside COM*/ sizeof(ContentStr) = 36 /*Inside DLL*/ sizeof(ContentStr) = 32 I could see that the difference in size is due to the difference in vector size. Searching the internet gave me huge confusion hence I am posting this question on the expert forum. Could someone please explain me why is this difference seen in the sizes and help me find a solution.
Kings
I don't see how
std::vector m_v_Data;
will compile; vector is a template class and needs a type to define it. [edit] In VS2010 I get 20 for the size of avector<int>
, and in VS2013 I get 16. There must be some other difference in your DLL code that reduces the total by another 4 bytes. [/edit] -
I have the following structure defined inside my DLL and a COM component we developed. struct ContentStr { ContentStr():ptr(NULL), contSize(0), structSize(0), X(0), Y(0){} const BYTE* ptr; int contSize; int structSize; //output std::vector m_v_Data; int X; int Y; private: ContentStr ( const ContentStr & rhs ); ContentStr & operator= ( const ContentStr & rhs ); }; The COM component invokes a DllExport method in the DLL which accepts the structure as reference and fills its value. It was working fine in VS2008. Recently I ported my code base to VS2013. Now the method throws exception when invoked. When I checked the size of the structure using sizeof operator, I was surprised a bit. VS 2008: /*Inside COM*/ sizeof(ContentStr) = 40 /*Inside DLL*/ sizeof(ContentStr) = 40 VS 2013: /*Inside COM*/ sizeof(ContentStr) = 36 /*Inside DLL*/ sizeof(ContentStr) = 32 I could see that the difference in size is due to the difference in vector size. Searching the internet gave me huge confusion hence I am posting this question on the expert forum. Could someone please explain me why is this difference seen in the sizes and help me find a solution.
Kings
Check if the VS2008 project is 64bit while the VS2013 one is 32bit.
-
I have the following structure defined inside my DLL and a COM component we developed. struct ContentStr { ContentStr():ptr(NULL), contSize(0), structSize(0), X(0), Y(0){} const BYTE* ptr; int contSize; int structSize; //output std::vector m_v_Data; int X; int Y; private: ContentStr ( const ContentStr & rhs ); ContentStr & operator= ( const ContentStr & rhs ); }; The COM component invokes a DllExport method in the DLL which accepts the structure as reference and fills its value. It was working fine in VS2008. Recently I ported my code base to VS2013. Now the method throws exception when invoked. When I checked the size of the structure using sizeof operator, I was surprised a bit. VS 2008: /*Inside COM*/ sizeof(ContentStr) = 40 /*Inside DLL*/ sizeof(ContentStr) = 40 VS 2013: /*Inside COM*/ sizeof(ContentStr) = 36 /*Inside DLL*/ sizeof(ContentStr) = 32 I could see that the difference in size is due to the difference in vector size. Searching the internet gave me huge confusion hence I am posting this question on the expert forum. Could someone please explain me why is this difference seen in the sizes and help me find a solution.
Kings
It's very bad idea to use the complex types from one DLL to another. Firstly your DLLs can use different "packing size". Secondly these STL types can use own allocators which can be different. Thirdly these STL types can be incompatible due to different realizations of STL.
With best wishes, Vita
-
I don't see how
std::vector m_v_Data;
will compile; vector is a template class and needs a type to define it. [edit] In VS2010 I get 20 for the size of avector<int>
, and in VS2013 I get 16. There must be some other difference in your DLL code that reduces the total by another 4 bytes. [/edit]it is vector of BYTE, sorry the code got modified when entered in the editor
Kings
-
It's very bad idea to use the complex types from one DLL to another. Firstly your DLLs can use different "packing size". Secondly these STL types can use own allocators which can be different. Thirdly these STL types can be incompatible due to different realizations of STL.
With best wishes, Vita
Could you please suggest me how can I make the packing size as same between COM and the DLL?
Kings
-
Check if the VS2008 project is 64bit while the VS2013 one is 32bit.
No, both the DLL and COM are compiled in VS2013 and are compiled as 32 bit binaries.
Kings
-
it is vector of BYTE, sorry the code got modified when entered in the editor
Kings
My apologies also, I think your code got mangled by the HTML tags, you need to use <pre> tags around it (use the code button above the edit window) so it shows up, like:
//output
std::vector m_v_Data;
int X;
int Y;There is obviously some difference between your application and your DLL. Are they both compiled with the exact same definition for your structure, i.e. using a common header file?
-
My apologies also, I think your code got mangled by the HTML tags, you need to use <pre> tags around it (use the code button above the edit window) so it shows up, like:
//output
std::vector m_v_Data;
int X;
int Y;There is obviously some difference between your application and your DLL. Are they both compiled with the exact same definition for your structure, i.e. using a common header file?
No they are not, the DLL contains a Util method which is exposed using DLLExport and the COM loads the DLL and creates a method pointer out of it and asks it to fill the data.
Kings
-
No they are not, the DLL contains a Util method which is exposed using DLLExport and the COM loads the DLL and creates a method pointer out of it and asks it to fill the data.
Kings
-
Well you need to show us the exact code in the application and the DLL, so we can try to figure out what the difference may be.
I am afraid that could not be done, however if you could point me to the direction where I can start digging through or help me with how you wud have approached this issue it wud be helpful.
Kings
-
I am afraid that could not be done, however if you could point me to the direction where I can start digging through or help me with how you wud have approached this issue it wud be helpful.
Kings
-
I have the following structure defined inside my DLL and a COM component we developed. struct ContentStr { ContentStr():ptr(NULL), contSize(0), structSize(0), X(0), Y(0){} const BYTE* ptr; int contSize; int structSize; //output std::vector m_v_Data; int X; int Y; private: ContentStr ( const ContentStr & rhs ); ContentStr & operator= ( const ContentStr & rhs ); }; The COM component invokes a DllExport method in the DLL which accepts the structure as reference and fills its value. It was working fine in VS2008. Recently I ported my code base to VS2013. Now the method throws exception when invoked. When I checked the size of the structure using sizeof operator, I was surprised a bit. VS 2008: /*Inside COM*/ sizeof(ContentStr) = 40 /*Inside DLL*/ sizeof(ContentStr) = 40 VS 2013: /*Inside COM*/ sizeof(ContentStr) = 36 /*Inside DLL*/ sizeof(ContentStr) = 32 I could see that the difference in size is due to the difference in vector size. Searching the internet gave me huge confusion hence I am posting this question on the expert forum. Could someone please explain me why is this difference seen in the sizes and help me find a solution.
Kings
_SECURE_SCL was enabled in COM amounting to 4 byte increase in size. Removing that solved the issue.
Kings