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

S

S Austin

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
 
V

Victor Bazarov

S Austin said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top