分段错误由c ++头文件中的空引用驱动

时间:2014-02-15 06:32:39

标签: c++ linux segmentation-fault nullreferenceexception

任何人都可以找到以下代码部分的错误:

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)
        {
....

1 个答案:

答案 0 :(得分:0)

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

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**  

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