2015-12-10 120 views
1

環境: - 的GNU/Linux(Ubuntu的14.04和Mageia 5) - GCC 4.9.2(Mageia下) - 系統QT5和提高爲什麼我的C++程序在發佈模式下崩潰時,結構有默認的構造函數?

截至最近我的計劃是在調試和發佈沒有問題的工作模式。無關的變化(並沒有很好地識別)使其崩潰,但僅在發佈模式下。不在調試中。

在調試中,valgrind不會發出任何錯誤信號。在發行版中,它會報告使用非初始化數據,但在方法開始時使用。通過系統的搜索,我能追查到使用下面的結構組成:

struct LIMA_LINGUISTICANALYSISSTRUCTURE_EXPORT LinguisticElement { 
    StringsPoolIndex inflectedForm; 
    StringsPoolIndex lemma; 
    StringsPoolIndex normalizedForm; 
    LinguisticCode properties; 
    MorphoSyntacticType type; 
    bool operator==(const LinguisticElement& le) const; 
    bool operator<(const LinguisticElement& le) const; 
}; 

StringsPoolIndex和LinguisticCode被定義爲:

BOOST_STRONG_TYPEDEF(uint64_t, StringsPoolIndex); 
BOOST_STRONG_TYPEDEF(uint32_t, LinguisticCode); 

和MorphoSyntacticType是一個枚舉。

如果我添加顯式構造函數和一個運算符=,崩潰消失,valgrind停止發出錯誤信號。

LinguisticElement::LinguisticElement() : 
    inflectedForm(0), 
    lemma(0), 
    normalizedForm(0), 
    properties(0), 
    type(NO_MORPHOSYNTACTICTYPE) 

{ 
} 
LinguisticElement::LinguisticElement(const LinguisticElement& le) : 
    inflectedForm(le.inflectedForm), 
    lemma(le.lemma), 
    normalizedForm(le.normalizedForm), 
    properties(le.properties), 
    type(le.type) 
{ 
} 
LinguisticElement& LinguisticElement::operator=(const LinguisticElement& le) 
{ 
    inflectedForm = le.inflectedForm; 
    lemma = le.lemma; 
    normalizedForm = le.normalizedForm; 
    properties = le.properties; 
    type = le.type; 
    return *this; 
} 

我不明白爲什麼會發生這種情況,因爲如果我理解的很好,我的實現與編譯器生成的實現相同。或者我錯了?

+0

發佈和調試模式在編譯器優化方面有所不同。在發佈模式中禁用優化,然後重試。 – adlag

+1

編譯器生成的構造函數不會初始化結構成員。 –

回答

2

您已將StringsPoolIndexLinguisticCode定義爲固定寬度整數類型。因此,它們將不會由您的結構體的編譯器合成構造函數初始化。變量通常在調試模式下初始化爲空(或者某些罕見的罕見特定值),而除非明確說明,否則在釋放模式下不會發生變化。這就是爲什麼你只在發佈版本配置中遇到崩潰。

+0

謝謝。我確信,使用buit-in類型的成員在編譯器合成的構造函數中始終是null初始化的!我將不得不檢查我所有的代碼... – Kleag

相關問題