如何修复Microsoft typeinfo.name()内存泄漏? [英] How to remediate Microsoft typeinfo.name() memory leaks?

查看:110
本文介绍了如何修复Microsoft typeinfo.name()内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft在调试版本中使用其泄漏检查工具时存在数十年的错误.当使用C ++类型信息(例如typeinfo.name())时,泄漏是由运行时库进行的分配报告的.另请参见调试CRT在typeinfo.name()内部报告的内存泄漏在Microsoft Connect上.

Microsoft has a decades old bug when using its leak check gear in debug builds. The leak is reported from the allocation made by the runtime library when using C++ type information, like typeinfo.name(). Also see Memory leaks reported by debug CRT inside typeinfo.name() on Microsoft Connect.

由于泄漏的时间大约相同,我们一直在收到错误报告和用户列表讨论. Microsoft错误还可能掩盖用户程序的实际泄漏.后一点对我来说尤其令人担忧,因为我们可能不会因为掩盖而趋于解决实际问题.

We've been getting error reports and user list discussions because of the leaks for about the same amount of time. The Microsoft bug could also mask real leaks from user programs. The latter point is especially worrisome to me because we may not tending to real problems because of the masking.

由于使用typeid(T)typeinfo.name(),我想尝试压缩泄漏.我的问题是,如何解决Microsoft的错误?有没有解决的办法?

I'd like to try to squash the leaks due to use of typeid(T) and typeinfo.name(). My question is, how can we work around Microsoft's bug? Is there a work around available?

推荐答案

在Q的评论中,我的建议行.

On the line of my suggestion in the Q's comments.

对于if (valueType == typeid(int)),您可以使用 type_index (至少自C ++ 11起)

For if (valueType == typeid(int)) you can use the type_index (at least since C++11)

对于type_info.name()内存泄漏:

由于似乎不可能完全消除泄漏,因此下一个最好的办法是减少它们的数量(每个类型询问仅减少一个泄漏),其次,为报告目的对其进行标记.
在某些模板化类中,人们可以希望内存泄漏"报告将使用类名(或至少是发生分配的源文件)-您可以随后使用此信息从所有内存泄漏的报告.

Since totally eliminating the leak doesn't seem possible, the next best thing would be reduce their number (to only one leaked per type interrogation) and, secondary, to tag them for reporting purposes.
Being inside some templated classes, one can hope that the 'mem leaking' report will use the class names (or at least the source file where the allocation happened) - you can subsequently use this information to filter them out from the 'all leaked memory' reports.

因此,您无需使用typeid(<typename>),而是使用类似的内容:

So instead of using typeid(<typename>), you use something like:

文件typeid_name_workaround.hpp"

"file typeid_name_workaround.hpp"

template <typename T> struct get_type_name {
  static const char* name() const {
    static const char* ret=typeid(T).name();
    return ret;
  }
};

其他.cpp/.hpp文件

#include "typeid_name_workaround.hpp"

struct dummy {
};

int main() {
  // instead of typeid(dummy).name() you use
  get_type_name<dummy>::name();
}

这篇关于如何修复Microsoft typeinfo.name()内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆