CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    sizeof(myclass) acting strangely.

    Hello,

    I have encountered a serious problem in my mode and was hoping someone may have a suggestion.

    I have a 3rd party library (cringe) and I have used it successfully in many projects. For the last 2 weeks I have been looking for a mysterious crash and I found the cause now.

    I have a MDI application and the control is initialized in a form, derived from CFormView for a child window. Everything works great. In fact, anywhere in the whole application I call sizeof(theclass) I get the proper size.

    Except my DOC class for the project. Whenever I access a couple of public variables from the DOC I get crashes. Turns out if I get the size of the class, it is 48 bytes less than anywhere else in the application. This causes all my data members to be completely wrong and in the wrong locations.

    I have looked at the assembly code and sure enough only in the DOC class is the wrong offsets being added to the member variable references which I expected.

    I have tried forcing padding and alignment but no luck. I have the sources for the 3rd party library but nothing in there forces padding or alignment. And as I say I use this library successfully in many places and I always use it the same way.

    Does anyone have even a slight hint where I should start looking for this problem?

    Thanks.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: sizeof(myclass) acting strangely.

    You can't take the size of a class. Period. The fact that it sometimes works is pure luck.

    Why do you need the size of a class anyway ?
    Last edited by Skizmo; May 5th, 2009 at 12:09 PM.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: sizeof(myclass) acting strangely.

    I think the more important question is why you need the size of your doc class.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sizeof(myclass) acting strangely.

    Quote Originally Posted by Skizmo
    You can't take the size of a class. Period. The fact that it sometimes works is pure luck.
    No, you can take the size of the class. It is not a matter of luck. What you cannot safely do is rely on the size obtained.

    Quote Originally Posted by Skizmo
    Why do you need the size of a class anyway ?
    Quote Originally Posted by GCDEF
    I think the more important question is why you need the size of your doc class.
    My impression is that Radius checked the size of the class in an attempt to find the cause of the crash.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    Re: sizeof(myclass) acting strangely.

    Ok sorry, maybe I didn't describe it well enough, I'm a little fried at the moment.

    I mean the size of the class definition not the declaration, so MY_CLASS myclass;

    I do sizeof(MY_CLASS);

    The reason fir this is because I discovered that getting a pointer to a member variable in the form class consistently returned a value 48 bytes less than the position it was actually stored at. So in the form class I get the memory location of an int following MY_CLASS and it is different than getting the location from the DOC class.

    The DOC class always assumes the member variables following MY_CLASS are 48 bytes less than their actual position. The sizeof() check was a last ditch attempt to see if something funny was going on with the actual class itself.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: sizeof(myclass) acting strangely.

    Is the mystery class being built at the same time, or is it something pre-built that's just now being linked in?

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: sizeof(myclass) acting strangely.

    Quote Originally Posted by Radius View Post
    The reason fir this is because I discovered that getting a pointer to a member variable in the form class consistently returned a value 48 bytes less than the position it was actually stored at. S
    Why are you doing it that way? Sounds like you're trying to get pointers to members using offsets. That's an incredibly bad idea.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sizeof(myclass) acting strangely.

    Quote Originally Posted by Radius
    The DOC class always assumes the member variables following MY_CLASS are 48 bytes less than their actual position.
    Do you say this as an observation, or as a note on the implementation?

    I think that you need to clarify what the implementation does and the steps you took to debug. At the moment the difference is ambiguous.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    Re: sizeof(myclass) acting strangely.

    The library is built in the same project, the build order includes it as the first item like all my other projects.

    Quote Originally Posted by GCDEF View Post
    Why are you doing it that way? Sounds like you're trying to get pointers to members using offsets. That's an incredibly bad idea.
    Here's what I do:

    Form class: int *x = &iVar; // Let's say it == 0x530 for fun.

    DOC class: int *x = &myForm->iVar; // Now it is 0x500.

    But this only applies to member variables after the 3rd party class. And ONLY applies to the DOC class. All other classes in the application do not exhibit this behavior.

  10. #10
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    Re: sizeof(myclass) acting strangely.

    Quote Originally Posted by laserlight View Post
    Do you say this as an observation, or as a note on the implementation?

    I think that you need to clarify what the implementation does and the steps you took to debug. At the moment the difference is ambiguous.
    Direct observation getting the pointers to the member variables.

  11. #11
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    Re: sizeof(myclass) acting strangely.

    Sorry, if this is too unclear I can post some more robust code. I was trying to keep it simple as not too flood with too much useless info but maybe that was not the best approach.

    [edit]

    Another thing to note, I have looked at the assembly code for this, and I can see in the DOC class only the offsets to the member variables are completely wrong. In ASM, the pointer to the class is loaded, and then a hard coded offset is added in order to load the data at the member variable.

    Pseudo code:

    Mov eax, <the pointer to the form class>
    Add eax, 0BA4h // references iVar
    // do some other stuff.

    But only in the DOC class is this number wrong, 0x30 bytes less than it should be. It is as though the DOC class is thinking the member variables are at a different offset than they really are. This is what has me so confused.
    Last edited by Radius; May 5th, 2009 at 12:37 PM.

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: sizeof(myclass) acting strangely.

    Quote Originally Posted by Radius View Post
    The library is built in the same project, the build order includes it as the first item like all my other projects.



    Here's what I do:

    Form class: int *x = &iVar; // Let's say it == 0x530 for fun.

    DOC class: int *x = &myForm->iVar; // Now it is 0x500.

    But this only applies to member variables after the 3rd party class. And ONLY applies to the DOC class. All other classes in the application do not exhibit this behavior.
    Are you sure you only have one doc object? Check the doc pointer and the this pointer from inside the doc and make sure you're looking at the same object.

  13. #13
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    Re: sizeof(myclass) acting strangely.

    Quote Originally Posted by GCDEF View Post
    Are you sure you only have one doc object? Check the doc pointer and the this pointer from inside the doc and make sure you're looking at the same object.
    Yes, I have not only checked that but every class pointer, member var and even gone so far as checking hWnd values for all relevant member variables and parent classes. I have been at this for quite some time now.

  14. #14
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: sizeof(myclass) acting strangely.

    Is the third party library a DLL?
    Are you passing C++ objects from your code to the third party code?

    Sounds like a case of memory corruption to me.

  15. #15
    Join Date
    Sep 2001
    Location
    Victoria, BC, Canada
    Posts
    363

    Re: sizeof(myclass) acting strangely.

    It is a 3rd party library, but only interfacing using regular function calls and nothing else.

    I thought memory corruption too but no memory checkers are alerting on anything, and the fact that the wrong offsets are compiled into the assembly code is really making me wonder if I haven't somehow made this particular DOC class think it's padded or aligned differently.

    Anyhow, I am exploring all options here. I hope it is just a simple compiler switch I missed or something. The usual culprit anyhow.

    Thanks everyone for the input!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured