VS2008 到 VS 2010 的迁移 - 重大变化?
我在将 C++ 代码从 VS2008 迁移到 VS2010 时遇到了问题。到目前为止我找不到原因的解释,非常感谢您的帮助。
我们有一个自定义内存分配器,它驻留在 dll 中。在代码的其余部分中,我们使用预处理器将分配重定向到我们的函数。下面的简单代码在 VS2008 中可以正确编译,但在 VS2010 中却不能。
在 stdafh.h 中:
#define free my_free
#include <string>
在 VS2010 中我得到:
1>d:\program files\microsoft visual studio 10.0\vc\include\xdebug(62): error C3861: 'free': identifier not found
来自线路:
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);
任何帮助或想法将受到高度赞赏!
摩西
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 C++ ISO 标准,第 17.4.3.1.1.2 节:
这意味着
#define
库函数名称具有其他含义是不合法的。我想这恰好可以在 VS2008 中工作,但是当迁移到 VS2010 时,编译器作者提出了一个不能正常工作的实现。如果您想重新定义
free
的功能,我建议您通过更传统的渠道来完成此操作,方法是将代码链接到您自己的 C 库实现,从而更改默认行为。According to the C++ ISO standard, section 17.4.3.1.1.2:
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.经过使用 /P 选项创建预处理器文件并研究它们的一些调查后,我找到了问题的根本原因。
xdebug 标头将自己描述为“Microsoft 的调试堆支持标头”,包含以下几行:
这显然挫败了我们重新定义它的尝试。所以这对编译器来说并不是什么新鲜事,只是简单的 #undef 恰好发生在我们尝试重新定义的函数中
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:
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