【问题标题】:Segmentation fault driven of a null reference in a c++ header filec++ 头文件中空引用驱动的分段错误
【发布时间】:2014-03-14 16:37:58
【问题描述】:

任何人都可以找出以下代码部分的问题:

private:
     TUcdFileReader& operator = (const TUcdFileReader& r) { Fail; return *((TUcdFileReader *) 0); }
     TUcdFileReader(const TUcdFileReader& r) { Fail; }

这属于我在代码中使用的 c 库 (SNAP) 中的头文件。当我在基于 linux 的系统中编译代码时,会出现以下警告:

Snap-2.1/glib-core/unicode.h(1678): warning #327: NULL reference is not allowed
                TUcdFileReader& operator = (const TUcdFileReader& r) { Fail; return *((TUcdFileReader *) 0); }

这总体上导致我出现如下分段错误:(我怀疑这是因为上述警告)

WARNING: Job died due to SIGSEGV - Invalid memory reference

我不写整个头文件,因为它很长而且容易混淆,但它是它的地址:http://snap.stanford.edu/snap/doc/snapuser-ref/dd/d90/unicode_8h_source.html 这里我写了大部分使用 TUcdFileReader 的头文件:

protected:

class TUcdFileReader
{
protected:
    TChA buf;
public:
    TChA comment; // contains '#' and everything after it
protected:
    FILE *f;
    int putBackCh;
    int GetCh() {
        if (putBackCh >= 0) { int c = putBackCh; putBackCh = EOF; return c; }
        return fgetc(f); }
    void PutBack(int c) { Assert(putBackCh == EOF); putBackCh = c; }
    // Returns 'false' iff the EOF was encountered before anything was read.
    bool ReadNextLine() {
        buf.Clr(); comment.Clr();
        bool inComment = false, first = true;
        while (true) {
            int c = GetCh();
            if (c == EOF) return ! first;
            else if (c == 13) {
                c = GetCh(); if (c != 10) PutBack(c);
                return true; }
            else if (c == 10) return true;
            else if (c == '#') inComment = true;
            if (! inComment) buf += char(c);
            else comment += char(c); }
            /*first = false;*/}
private:
    TUcdFileReader& operator = (const TUcdFileReader& r) { Fail; return *((TUcdFileReader *) 0); }
    TUcdFileReader(const TUcdFileReader& r) { Fail; }
public:
    TUcdFileReader() : f(0) { }
    TUcdFileReader(const TStr& fileName) : f(0), putBackCh(EOF) { Open(fileName); }
    void Open(const TStr& fileName) { Close(); f = fopen(fileName.CStr(), "rt"); IAssertR(f, fileName); putBackCh = EOF; }
    void Close() { putBackCh = EOF; if (f) { fclose(f); f = 0; }}
    ~TUcdFileReader() { Close(); }
    bool GetNextLine(TStrV& dest) {
        dest.Clr();
        while (true) {
            if (! ReadNextLine()) return false;
            TStr line = buf; line.ToTrunc();
            if (line.Len() <= 0) continue;
            line.SplitOnAllCh(';', dest, false);
            for (int i = 0; i < dest.Len(); i++) dest[i].ToTrunc();
            return true; }}
    static int ParseCodePoint(const TStr& s) {
        int c; bool ok = s.IsHexInt(true, 0, 0x10ffff, c); IAssertR(ok, s); return c; }
    static void ParseCodePointList(const TStr& s, TIntV& dest, bool ClrDestP = true) { // space-separated list
        if (ClrDestP) dest.Clr();
        TStrV parts; s.SplitOnWs(parts);
        for (int i = 0; i < parts.Len(); i++) {
            int c; bool ok = parts[i].IsHexInt(true, 0, 0x10ffff, c); IAssertR(ok, s);
            dest.Add(c); } }
    static void ParseCodePointRange(const TStr& s, int& from, int &to) { // xxxx or xxxx..yyyy
        int i = s.SearchStr(".."); if (i < 0) { from = ParseCodePoint(s); to = from; return; }
        from = ParseCodePoint(s.GetSubStr(0, i - 1));
        to = ParseCodePoint(s.GetSubStr(i + 2, s.Len() - 1)); }
};

此外,唯一调用 TUcdFileReader 的地方:

class TSubcatHelper
    {
    public:
        bool hasCat; TUniChSubCategory subCat;
        TStrH invalidCatCodes;
        TUniChDb &owner;

        TSubcatHelper(TUniChDb &owner_) : owner(owner_) { }

        void ProcessComment(TUniChDb::TUcdFileReader &reader)
        {
....

【问题讨论】:

标签: c++ linux segmentation-fault nullreferenceexception


【解决方案1】:

TUcdFileReader 类具体定义为没有具有赋值或复制构造函数,即

private:
    TUcdFileReader& operator = (const TUcdFileReader& r) 
               { Fail; return *((TUcdFileReader *) 0); }
    TUcdFileReader(const TUcdFileReader& r) { Fail; }

看起来他们注定要失败。

它们被调用的代码中的某处 - 如果您将它们替换为:

private:
    TUcdFileReader& operator = (const TUcdFileReader& r); //**not implemented** 
    TUcdFileReader(const TUcdFileReader& r);              //**not implemented**  

然后你会得到一个链接器错误。最好的解决办法是(如果可能的话)实施它们。

【讨论】:

  • 唯一调用 TUcdFileReader 的地方是:void ProcessComment(TUniChDb::TUcdFileReader &reader) 我在我的问题中更新了这部分。感谢您的提示,但我不知道如何解决此问题。
  • 假设 C++11,您甚至可以像这样标记您不需要的构造函数 = deleteTUcdFileReader&amp; operator = (const TUcdFileReader&amp; r) = delete; 以获得编译错误而不是链接器错误(您之前注意到)。
  • @Martin J:不知道——真的很有用,ta。
猜你喜欢
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多