Inline code in header files - learned my lesson, but one mystery remains

Discussion in 'C++' started by S Austin, Jan 29, 2004.

  1. S Austin

    S Austin Guest

    Discovered recently (duh) that putting inline code in .h files (e.g.
    in class definitions) is not a good idea when building DLLs and the
    applications that use those DLLs. The reason being, of course, is
    that the application gets its own copy of that code when it compiles
    and won't call the code in the DLL. Each compiled unit in the DLL also
    ends up with its own copy of the never-called code.

    Assuming the same build process for the DLL and the application,
    theoretically this shouldn't be that big a deal...however, I only
    discovered the error of my ways when the program didn't run right. It
    turns out that the offsets into member variables were getting computed
    differently by the compiler when it built the DLL and the application.
    In other words, the inline function code built into the application
    didn't use the same offsets to member variables as the code in other
    functions implemented in the DLL.

    I can't explain why this would be the case?

    The specific case I encountered was that the address stored into a
    32-bit pointer was different by one byte. Certainly strange enough to
    think that an odd-numbered address would ever be computed by the
    compiler when there were no individual char's in the class and all
    char arrays had power-of-two lengths.

    Stepping through the machine code in the debugger revealed the bug -
    the constant offset added to the base register was 12 in functions
    within the DLL and 13 within the inline functions in the application.
    Needless to say jumping into a data structure one byte past its
    starting address screwed things up royally.

    FYI BCB 5.0 on Win XP.

    Cheers
    S. Austin
     
    S Austin, Jan 29, 2004
    #1
    1. Advertising

  2. "S Austin" <> wrote...
    > Discovered recently (duh) [...]
    > [...] the inline function code built into the application
    > didn't use the same offsets to member variables as the code in other
    > functions implemented in the DLL.
    >
    > I can't explain why this would be the case?


    Is that a question? I don't know whether you can or cannot.
    The simplest answer is that you used different compiler options
    when compiling your executable and your library. They do need to
    be synchronised rather carefully, you know. It's a big PITA, if
    you ask me.

    > [...]
    > FYI BCB 5.0 on Win XP.


    I would ask in the compiler newsgroup, luckily there is one.

    Victor
     
    Victor Bazarov, Jan 29, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nish
    Replies:
    4
    Views:
    545
    Thomas Stegen
    Oct 8, 2004
  2. Rahul
    Replies:
    3
    Views:
    480
    James Kanze
    Feb 28, 2008
  3. mlt
    Replies:
    2
    Views:
    916
    Jean-Marc Bourguet
    Jan 31, 2009
  4. Richard Lawrence
    Replies:
    1
    Views:
    90
  5. Sven Wolf
    Replies:
    7
    Views:
    223
    Tad McClellan
    Dec 3, 2004
Loading...

Share This Page