C ++中不稳定错误的最常见原因是什么? [英] Most common reasons for unstable bugs in C++?

查看:143
本文介绍了C ++中不稳定错误的最常见原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个大型项目,我大部分时间都在调试。虽然调试是一个正常的过程,有错误,是不稳定的,这些错误是开发人员最大的痛苦。程序不工作,好,有时...有时它会,并且没有什么你能做的。



这些错误可以做什么?最常见的调试工具(交互式调试器,手表,日志消息)可能会导致你无处可去,因为错误会消失...只是再次出现,稍后。这就是为什么我要求一些启发式:这些错误的最常见的原因是什么?我们应该调查哪些可疑代码以找到此类错误?



让我开始列表:


  1. 使用未初始化的变量。
    常见的错误印记如mMember =
    mMember;

  2. 线程同步。
    有时它可能是一个问题
    运气;

  3. 使用非智能
    指针,取消引用无效的
    指针;

还有什么?

解决方案

IME许多项目中的潜在问题是开发人员使用C ++的低级功能,内存管理,C风格字符串处理等等,即使它们很少是必要的(然后只有很好地封装在类中)。这导致内存损坏,无效指针,缓冲区溢出,资源泄漏和什么。所有的同时漂亮和干净的高级结构可用。



我是几年来一个大型(几个MLoC)应用程序的一部分,并且应用程序的不同部分的崩溃错误数量与编程风格有很好的相关性用于这些部件。当被问及为什么不改变他们的编程风格时,一些罪犯回答说他们的风格一般会产生更多的性能。 (不仅仅是这个错误,这也是一个事实,客户宁愿有一个更稳定,但较慢的程序,而不是一个快速的,它保持崩溃他们,而且,他们的大多数代码甚至不需要快速...) / p>

至于多线程:我感觉不够专业,不能在这里提供解决方案,但我认为 Herb Sutter的有效并发列是一个非常值得阅读的主题。



编辑以处理评论中的讨论



C风格的字符串处理不是更高效。 (当然在这句话中有很多否定的,但是因为我觉得误解了,所以我尽量精确。)我说的是高层次的结构通常不是性能更好的: std :: vector 通常比手动执行动态分配的C数组慢,因为是动态分配的C数组。当然,存在根据特殊要求编码的东西将比任何一般解决方案执行得更好的情况 - 但是这不一定意味着您将必须诉诸手动内存管理。这就是为什么我写的,如果这样的事情是必要的,那么只有很好地封装在类中。



但更重要的是:在大多数代码中,差异并不重要。无论一个按钮按下0.01秒后,有人点击它或0.05秒简单无关紧要,所以即使一个因素5速度增益是不相关的按钮的代码。然而,代码是否崩溃,总是 重要。



总结我的参数:首先让它正常工作。这是最好的做法使用成熟的现成的积木。然后测量。然后使用成熟的现成习语改善性能。


I am currently working on a large project, and I spend most of the time debugging. While debugging is a normal process, there are bugs, that are unstable, and these bugs are the greatest pain for the developer. The program does not work, well, sometimes... Sometimes it does, and there is nothing you can do about it.

What can be done about these bugs? Most common debugging tools (interactive debuggers, watches, log messages) may lead you nowhere, because the bug will disappear ... just to appear once again, later. That is why I am asking for some heuristics: what are the most common reasons for such bugs? What suspicious code should we investigate to locate such a bugs?

Let me start the list:

  1. using uninitialized variables. Common misprints like mMember = mMember;
  2. thread synchronization. Sometimes it can be a matter of luck;
  3. working with non-smart pointers, dereferencing invalid ones;

what else?

解决方案

IME the underlying problem in many projects is that developers use low-level features of C++ like manual memory management, C-style string handling, etc. even though they are very rarely ever necessary (and then only well encapsulated in classes). This leads to memory corruption, invalid pointers, buffer overflows, resource leaks and whatnot. All the while nice and clean high-level constructs are available.

I was part of the team for a large (several MLoC) application for several years and the number of crashing bugs for different parts of the application nicely correlated to the programming style used within these parts. When asked why they wouldn't change their programming style some of the culprits answered that their style in general yields more performance. (Not only is this wrong, it's also a fact that customers rather have a more stable but slower program than a fast one that keeps crashing on them. Also, most of their code wasn't even required to be fast...)

As for multi-threading: I don't feel expert enough to offer solutions here, but I think Herb Sutter's Effective Concurrency columns are a very worthwhile read on the subject.

Edit to address the discussions in the comments:

I did not write that "C-style string handling is not more performant". (Certainly a lot of negation in this sentence, but since I feel misread, I try to be precise.) What I said is that high level constructs are not in general less performant: std::vector isn't in general slower than manually doing dynamically allocated C arrays, since it is a dynamically allocated C array. Of course, there are cases where something coded according to special requirements will perform better than any general solution -- but that doesn't necessarily mean you'll have to resort to manual memory management. This is why I wrote that, if such things are necessary, then only well-encapsulated in classes.

But what's even more important: in most code the difference doesn't matter. Whether a button depresses 0.01secs after someone clicked it or 0.05secs simply doesn't matter, so even a factor 5 speed gain is irrelevant in the button's code. Whether the code crashes, however, always matters.

To sum up my argument: First make it work correctly. This is best done using well-proven off-the-shelf building blocks. Then measure. Then improve performance where it matters, using well-proven off-the-shelf idioms.

这篇关于C ++中不稳定错误的最常见原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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