Re: const char* differences between VS6 & VS8
On Fri, 12 Dec 2008 07:33:05 -0800, "John Keenan"
<john.removeme.keenan@optimapowerware.com> wrote:
ABI compatibility issue
Application binary interface... I was not previously aware of that
particular acronyn but see now that searches for it provide useful
information.
returning a c_str() from an inline function... must use the same compiler
and compiler settings
Why would returning c_str() from an inline function cause a problem? I
thought a "C string" (const char*) was a built in type that is well defined
and well behaved.
But c_str() is a std::string member function, and the layout of std::string
differs rather wildly between VC6 and VC8, the former using reference
counting, and the latter using the small string optimization. So your EXE
and DLL disagree on what makes up a std::string, and using inline functions
that access std::string innards can easily expose this.
I originally thought the problem was a result of using a mix-match of debug
and release. However I made a small example and tried the 4 combinations of
debug and release and they all worked fine. Now my original problem has
disappeared... so I think I must have used a stale dll that was mistakenly
returning the std::string address as opposed to the address returned by
c_str().
Could be, but if you are going to mix VC6 and VC8 modules (EXE and DLLs),
you must take the module boundary very seriously, passing only objects of
basic types across it. You can't share C++ classes, copy std::strings,
delete objects defined in another module, and so forth. More generally,
that holds when using different CRT DLLs, non-DLL CRTs, different compiler
version, etc.
--
Doug Harrison
Visual C++ MVP