找出bad_alloc原因的调试策略 [英] Debugging strategy to find the cause of bad_alloc

查看:318
本文介绍了找出bad_alloc原因的调试策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中存在一个相当严重的错误-偶尔调用new()会抛出bad_alloc。

I have a fairly serious bug in my program - occasional calls to new() throw a bad_alloc.

从我可以在bad_alloc上找到的文档中看来,出于以下原因而被抛出:

From the documentation I can find on bad_alloc, it seems to be thrown for these reasons:


  1. 当计算机内存不足时(这绝对不会发生,我有4GB的内存) RAM,如果使用的内存不足5MB(已在taskmanager中检查),并且在后台没有任何严重运行,则程序将抛出bad_alloc。

  1. When the computer runs out of memory (which definitely isn't happening, I have 4GB of RAM, program throws bad_alloc when using less than 5MB (checked in taskmanager) with nothing serious running in the background).

如果内存过于分散而无法分配新块(这不太可能-我分配的最大大小的块大约为1KB,并且在崩溃发生之前完成的次数不超过100次)。

If the memory becomes too fragmented to allocate new blocks (which, again, is unlikely - the largest sized block I ever allocate would be about 1KB, and that doesn't get done more than 100 times before the crash occurs).

基于这些描述,我真的没有地方可以抛出bad_alloc。

Based on these descriptions, I don't really have anywhere in which a bad_alloc could be thrown.

但是,我正在运行的应用程序运行多个线程,这可能是导致问题的原因。通过在单个线程上测试所有对象,一切似乎都能顺利进行。我能想到的唯一的另一件事可能是某种竞争条件,该竞争条件是由于在多个地方同时调用new()而引起的,但是我尝试添加互斥量来防止这种行为。没效果。

However, the application I am running runs more than one thread, which could possibly be contributing to the problem. By testing all of the objects on a single thread, everything seems to be working smoothly. The only other thing that I can think of that is going on here could be some kind of race-condition caused by calling new() in more than one place at the same time, but I've tried adding mutexes to prevent that behaviour to no effect.

因为该程序有几百行,而且我不知道问题出在哪里,所以我不确定要使用什么代码片段发布。相反,我想知道是否有任何工具可以帮助我测试这种情况,或者是否有任何常规策略可以帮助我解决此问题。

Because the program is several hundred lines and I have no idea where the problem actually lies, I'm not sure of what, if any, code snippets to post. Instead, I was wondering if there were any tools that will help me test for this kind of thing, or if there are any general strategies that can help me with this problem.

>我使用的是Microsoft Visual Studio 2008,带有Poco进行线程处理。

I'm using Microsoft Visual Studio 2008, with Poco for threading.

推荐答案

另一个可能的问题是,尽管您提到该程序使用的内存不足5MB,您没有提及它试图分配多少空间。您可能会遇到某些竞争状况,该竞争状况会破坏用于确定分配大小的值,并且可能正试图分配37TB或类似的废话。

Another possible problem is that, while you mention that the program is using less than 5MB, you don't mention how much space it's trying to allocate. You could have some race condition that's corrupting the value that you use to determine the allocation size, and it could be trying to allocate 37TB or somesuch nonsense.

我想,但值得检查。

这篇关于找出bad_alloc原因的调试策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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