GRenard wrote:
Hello,
We just switch in our company to VisualStudio 2005 and the new ATL
library.
We use a wrapper to use CFileDialog. Its name is CFileDialogDeluxe
Here the call
CFileDialogDeluxe oFileDialog( ... );
There are some problems in the debug, look the sizeof from the debug :
sizeof(oFileDialog) 1144 unsigned int
sizeof(CFileDialogDeluxe) 1156 unsigned int
sizeof((*(WTL::CFileDialog*)(&oFileDialog))) 1156 unsigned int
Now the CFileDialogDeluxe just call the constructor of CFileDialog, I
removed all the members from our class.
But when I put members, there are some offsets on the address which
make the program crashes when destructing variables.
So what can affect the sizeof of a variable like that because it
inserts offset of variables.
Thank you very much
Jean-Sébastien Goupil
Padding is causing the offsets to change. Padding should never affect
the destruction of members. If it does, you've got undefined behaviour.
If you are allocating/deallocating members through pointers based on
the sizeof involved and their "offsets", you have undefined behaviour.
That is strictly forbidden in C++. Padding can change depending on
platform and compiler switches. A properly designed program remains
unaffected by these options.
Lets take a silly example:
#include <iostream>
struct A
{
int n;
char c;
};
int main()
{
A a;
std::cout << "sizeof(int) = " << sizeof(int) << std::endl;
std::cout << "sizeof(char) = " << sizeof(char) << std::endl;
std::cout << "sizeof(A) = " << sizeof(A) << std::endl;
}
/*
sizeof(int) = 4
sizeof(char) = 1
sizeof(A) = 8
*/
This is expected. The compiler on this particular platform padded the
class to match this computer's memory architecture.
If i had newed/malloc the instances of the class and/or members, its my
responsability to deallocate those objects and/or members via pointers
that are unaffected by the padding involved. Thats the law.