VC++ 中未初始化的内存块 [英] Uninitialized memory blocks in VC++

查看:39
本文介绍了VC++ 中未初始化的内存块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,Visual C++ 运行时标记未初始化或刚刚释放具有特殊非零标记的内存块.有没有办法完全禁用这种行为,而无需手动将所有未初始化的内存设置为零?它对我的有效非空检查造成了严重破坏,因为 0xFEEEFEEE != 0.

As everyone knows, the Visual C++ runtime marks uninitialized or just freed memory blocks with special non-zero markers. Is there any way to disable this behavior entirely without manually setting all uninitialized memory to zeros? It's causing havoc with my valid not null checks, since 0xFEEEFEEE != 0.

嗯,也许我应该解释得更好一点.我创建并初始化了一个变量(通过 new),一切都很好.当我释放它(通过删除)时,它将指针设置为 0xFEEEFEEE 而不是 NULL.当我插入对 NULL 的正确检查时,就像所有管理自己内存的优秀程序都应该的那样,当 0xFEEEFEEE 传递一个 NULL 时,我会遇到问题> 检查没有问题.除了在删除它们时手动将所有指针设置为 NULL 之外,还有什么好方法可以检测内存何时已被释放?我宁愿不使用 Boost 仅仅因为我不想要开销,尽管它可能很小,因为这是我唯一使用 Boost 的目的.

Hrm, perhaps I should explain a bit better. I create and initialize a variable (via new), and that all goes just fine. When I free it (via delete), it sets the pointer to 0xFEEEFEEE instead of NULL. When I insert a proper check for NULL, as all good programs that manage their own memory should, I come up with problems as 0xFEEEFEEE passes a NULL check without problems. Is there any good way, other than manually setting all pointers to NULL when deleting them, to detect when memory has already been freed? I would prefer to not use Boost simply because I don't want the overhead, small though it may be, since that's the only thing I'd be using Boost for.

推荐答案

delete 不负责将所有指向对象的指针重置为 NULL.此外,您不应该更改 Windows DEBUG 运行时的默认内存填充,并且应该以任何方式使用诸如 boost::shared_ptr<> 之类的东西作为指针.

It is not the responsibility of delete to reset all the pointers to the object to NULL. Also you shouldn't change the default memory fill for the windows DEBUG runtime and you should use some thing like boost::shared_ptr<> for pointers any way.

也就是说,如果你真的想用脚射击自己,你可以.

That said, if you really want to shoot your self in the foot you can.

您可以使用这样的分配器钩子更改 windows DEBUG 运行时默认填充.这仅适用于 HEAP 分配的对象!

You can change the default fill for the windows DEBUG runtime by using an allocator hook like this. This will only work on HEAP allocated object!

int main(int argc,char** arv)
{
  // Call first to register hook    
  _CrtSetAllocHook(&zero_fill);
  // Do other stuff
  malloc(100);
}


int zero_fill(int nAllocType, 
              void* pvData, 
              size_t nSize,
              int nBlockUse, 
              long lRequest, 
              const unsigned char *szFileName, 
              int nLine )
{
  /// Very Importaint !! 
  /// infinite recursion if this is removed !!
  /// _CRT_BLOCK must not do any thing but return TRUE
  /// even calling printf in the _CRT_BLOCK will cause
  /// infinite recursion
  if ( nBlockUse == _CRT_BLOCK )
    return( TRUE );
  switch(nAllocType)
  {
  case _HOOK_ALLOC:
  case _HOOK_REALLOC:
    // zero initialize the allocated space.
    memset(pvData,0,nSize);
    break;
  case _HOOK_FREE:
    break;
  }
  return TRUE;
}

这篇关于VC++ 中未初始化的内存块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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