Cpp程序在调试模式下正常,在常规模式就崩溃得情况

笔者前几天在使用cpp(c++)写代码得时候, 发现一个很蛋疼得问题, 在使用lldb进行调试得时候,程序完全正常,没有错误。 而退出调试模式, 使用命令行运行程序得时候, 则会触发内存错误。

经过一番查找之后发现, 笔者发现有些类得属性没有进行初始化。 。。

这可能是一个非常基础得错误。。 但是笔者一般写Java得时候 是不需要考虑这个问题得。。😂😂 😂

大致情况是这样得。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// 物品类, 占位使用
class Thing {
  
}

class Person {
public:
  void take(Thing* thing){
  	if( m_onHand == nullptr ){
      // do something ..
    }
    
    // do something ..
  }
private:
  // 手里正在拿得东西
  Thing* m_onHand;
}

在调试模式下, 上述代码得属性部分 m_onHand 是一个nullptr,这会产生预期的行为。

而非调试模式下,似乎就不是一个nullptr , 则会产生不可预期得内存错误。 😢😢😢

笔者使用得解决办法就是在声明得时候这么写: Thing* m_onHand = nullptr;

除此之外, 笔者还搜索到了一种可能性。

链接在此: https://stackoverflow.com/questions/186237/program-only-crashes-as-release-build-how-to-debug/186285#186285

下面是原文引用:

In 100% of the cases I’ve seen or heard of, where a C or C++ program runs fine in the debugger but fails when run outside, the cause has been writing past the end of a function local array. (The debugger puts more on the stack, so you’re less likely to overwrite something important.)

0%