Moshe Kravchik
Moshe Kravchik

Reputation: 2341

VS2008 to VS 2010 migration - a breaking change?

I encountered a problem when migrating our C++ code from VS2008 to VS2010. I can't find an explanation for the reason of it so far and will appreciate your help.

We have a custom memory allocator and it resides in a dll. In the rest of the code we use the preprocessor to redirect the allocations to our functions. Thie following simple code compiles correctly in VS2008, but does not in VS2010.

in stdafh.h:

#define free my_free
#include <string>

In VS2010 I get:

1>d:\program files\microsoft visual studio 10.0\vc\include\xdebug(62): error C3861: 'free': identifier not found

Coming from the line:

template<class _Ty>
    void __CLRCALL_OR_CDECL _DebugHeapDelete(_Ty *_Ptr)
    {   // delete from the debug CRT heap even if operator delete exists
    if (_Ptr != 0)
        {   // worth deleting
        _Ptr->~_Ty();
        // delete as _NORMAL_BLOCK, not _CRT_BLOCK, since we might have
        // facets allocated by normal new.
        free(_Ptr);

Any help or ideas will be highly appreciated!

Moshe

Upvotes: 2

Views: 1015

Answers (2)

Moshe Kravchik
Moshe Kravchik

Reputation: 2341

After some investigation using the /P option to create the preprocessor files and studying them I've found the root cause of the issue.

The xdebug header, which describes itself as "debug heap support header for Microsoft", contains the following lines:

#pragma push_macro("free")
#undef free

which obviously defeat our attempts to redefine it. So this is not something new to the compiler, just plain #undef that happens to occur with the functions we try to redefine

Upvotes: 2

templatetypedef
templatetypedef

Reputation: 372794

According to the C++ ISO standard, section 17.4.3.1.1.2:

A translation unit that includes a header shall not contain any macros that define names declared or defined in that header. Nor shall such a translation unit define macros for names lexically identical to keywords.

This means that it's not legal to #define a library function name to mean something else. I guess this just happened to work in VS2008, but when migrating to VS2010 the compiler authors came up with an implementation in which this does not work properly.

If you want to redefine what free does, I suggest doing it through a more conventional channel by linking the code against your own implementation of the C library that changes the default behavior.

Upvotes: 6

Related Questions