Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmentation fault -- How!

Tags:

c++

int Sequence::scoreDegeneracy()
{
    cout << "Score Degeneracy" << name << seqLen << endl;
    int f = 0;
    if (degenComputed == false)
    {
        char _2foldTest = '*';
        char _4foldTest = '*'; 
        int aaseqLen = seqLen/3;
        int i = 0;
        for (i; i < aaseqLen; i++)

        {
            _4foldTest = is4FoldDegenerateSite(i);
            _2foldTest = is2FoldDegenerateSite(i,_4foldTest);
            degScores.totalCodons++
                if (_2foldTest != '*')
            {
                degScores.totalCodons_2fold++;
                if (_2foldTest == 'A')
                {
                    degScores.total_2fold_A++;
                }
                            else if (_2foldTest == 'T')
                {
                    degScores.total_2fold_T++;
                }
                            else if (_2foldTest == 'G')
                {
                    degScores.total_2fold_G++;
                }
                             else
                {
                    degScores.total_2fold_C++;   
                }  
            }else if (_4foldTest != '*')
            {
                degScores.totalCodons_4fold++;
                if (_4foldTest == 'A')
                {
                    degScores.total_4fold_A++;
                }
                            else if (_4foldTest == 'T')
                {
                    degScores.total_4fold_T++;
                }
                            else if (_4foldTest == 'C')
                {
                    degScores.total_4fold_C++;
                }
                            else
                {
                    degScores.total_4fold_G++;
                }
            }
            cout << "Crashes right here when i = 0." << endl;        
        } 
    }
    degenComputed = true;
    return 1;
}

Driving me nuts! SegFault happens right at end of first iteration of for loop. Behaves EXACTLY like it supposed to, first test returns right results.. The segmentation fault happens before i is even incremented....

What am I missing??

...

char Sequence::is4FoldDegenerateSite(int codonIndex){
 char aminoAcid = aaSeq[codonIndex];
 //cout << "Amino acid of codon number " << codonIndex << " is: " << aminoAcid << endl;
 int loc = readingFrame + (codonIndex * 3) + 2;  
 int locR = loc - 2;
 switch (aminoAcid){
  case 'A':
   return seq[loc];
  case 'T':
   return seq[loc];
  case 'V':  
   return seq[loc];      
  case 'G':
   return seq[loc];
  case 'P':
   return seq[loc];
  case 'S'://Can also be 2 fold degenerate
   if (seq[locR] == 'T'){ 
    return seq[loc];
   }else{ 
    return '*';
   }    
  case 'R'://Can also be 2 fold degenerate
   if (seq[locR] == 'C'){
    return seq[loc];
   }else{ 
    return '*';
   }
  case 'L'://Can also be 2 fold degenerate
   if (seq[locR] == 'C'){ 
    return seq[loc];
   }else{ 
    return '*';   
   }   
 }  
 return '*';
}
char Sequence::is2FoldDegenerateSite(int codonIndex, char _4FoldResults){
 char aminoAcid = aaSeq[codonIndex];
 int loc = readingFrame + (codonIndex * 3) + 2;
 //char doubleDegeneracyTest = is4FoldDegenerateSite(codonIndex);
 //bool doubleDegeneracy = false;
 //if (doubleDegeneracyTest != '*') doubleDegeneracy = true;  
 switch (aminoAcid){
  case 'N':
   return seq[loc];
  case 'D':
   return seq[loc];
  case 'C':  
   return seq[loc];      
  case 'Q':
   return seq[loc];
  case 'E':
   return seq[loc];
  case 'H':
   return seq[loc];
  case 'K':
   return seq[loc];
  case 'F':
   return seq[loc];
  case 'Y':
   return seq[loc];
  case 'X':
   return seq[loc];
  case 'S'://Can also be 2 fold degenerate
   if (_4FoldResults == '*'){ 
    return seq[loc];
   }else{ 
    return '*';
   }    
  case 'R'://Can also be 2 fold degenerate
   if (_4FoldResults == '*'){
    return seq[loc];
   }else{ 
    return '*';
   }
  case 'L'://Can also be 2 fold degenerate
   if (_4FoldResults == '*'){ 
    return seq[loc];
   }else{ 
    return '*';   
   }   
 }  
 return '*';
}

Valgrind Results:

**17043** new/new[] failed and should throw an exception, but Valgrind
==17043==    at 0x4024E35: VALGRIND_PRINTF_BACKTRACE (valgrind.h:3720)
==17043==    by 0x40253C2: operator new(unsigned int) (vg_replace_malloc.c:214)
==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D70A7: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D7EDC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D83AF: std::string::append(std::string const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x804BA7F: std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:2168)
==17043==    by 0x804ACDF: GenomeSeq::setup() (GenomeSeq.C:108)
==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
==17043==    by 0x80496DF: main (Main2.C:143)
**17043**    cannot throw exceptions and so is aborting instead.  Sorry.
==17043==    at 0x4024E35: VALGRIND_PRINTF_BACKTRACE (valgrind.h:3720)
==17043==    by 0x40253D0: operator new(unsigned int) (vg_replace_malloc.c:214)
==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D70A7: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D7EDC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D83AF: std::string::append(std::string const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x804BA7F: std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:2168)
==17043==    by 0x804ACDF: GenomeSeq::setup() (GenomeSeq.C:108)
==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
==17043==    by 0x80496DF: main (Main2.C:143)
Loading sequence..==17043== 
==17043== HEAP SUMMARY:
==17043==     in use at exit: 2,749,679 bytes in 10 blocks
==17043==   total heap usage: 39,043 allocs, 39,032 frees, 80,022,672,644 bytes allocated
==17043== 
==17043== 27 bytes in 1 blocks are definitely lost in loss record 1 of 10
==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D7340: ??? (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D7525: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x804969E: main (Main2.C:143)
==17043== 
==17043== 28 bytes in 1 blocks are definitely lost in loss record 2 of 10
==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D7340: ??? (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D7525: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x80496C3: main (Main2.C:143)
==17043== 
==17043== 83 bytes in 1 blocks are definitely lost in loss record 3 of 10
==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D70A7: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D7EDC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D814A: std::string::append(char const*, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40B3F3E: std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40C9799: std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x804ACB5: GenomeSeq::setup() (GenomeSeq.C:106)
==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
==17043==    by 0x80496DF: main (Main2.C:143)
==17043== 
==17043== 100 bytes in 1 blocks are definitely lost in loss record 4 of 10
==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D70A7: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D7EDC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D814A: std::string::append(char const*, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40B3F3E: std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40C9799: std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x804AC07: GenomeSeq::setup() (GenomeSeq.C:101)
==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
==17043==    by 0x80496DF: main (Main2.C:143)
==17043== 
==17043== 100 bytes in 1 blocks are definitely lost in loss record 5 of 10
==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D7340: ??? (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D75D1: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D33C5: std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >::basic_istringstream(std::string const&, std::_Ios_Openmode) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x804AC42: GenomeSeq::setup() (GenomeSeq.C:103)
==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
==17043==    by 0x80496DF: main (Main2.C:143)
==17043== 
==17043== 8,192 bytes in 1 blocks are definitely lost in loss record 8 of 10
==17043==    at 0x4025024: operator new[](unsigned int) (vg_replace_malloc.c:258)
==17043==    by 0x40A2592: std::basic_filebuf<char, std::char_traits<char> >::_M_allocate_internal_buffer() (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40A65B1: std::basic_filebuf<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40A7EAF: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x804ABBF: GenomeSeq::setup() (GenomeSeq.C:97)
==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
==17043==    by 0x80496DF: main (Main2.C:143)
==17043== 
==17043== 8,192 bytes in 1 blocks are definitely lost in loss record 9 of 10
==17043==    at 0x4025024: operator new[](unsigned int) (vg_replace_malloc.c:258)
==17043==    by 0x40A2592: std::basic_filebuf<char, std::char_traits<char> >::_M_allocate_internal_buffer() (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40A65B1: std::basic_filebuf<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40A7EAF: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x804AC9D: GenomeSeq::setup() (GenomeSeq.C:105)
==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
==17043==    by 0x80496DF: main (Main2.C:143)
==17043== 
==17043== 2,732,253 bytes in 1 blocks are definitely lost in loss record 10 of 10
==17043==    at 0x4025390: operator new(unsigned int) (vg_replace_malloc.c:214)
==17043==    by 0x40D6435: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D70A7: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D7EDC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x40D83AF: std::string::append(std::string const&) (in /usr/lib/libstdc++.so.6.0.13)
==17043==    by 0x804BA7F: std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:2168)
==17043==    by 0x804ACDF: GenomeSeq::setup() (GenomeSeq.C:108)
==17043==    by 0x804A4FE: GenomeSeq::GenomeSeq(std::string, std::string) (GenomeSeq.C:52)
==17043==    by 0x80496DF: main (Main2.C:143)
==17043== 
==17043== LEAK SUMMARY:
==17043==    definitely lost: 2,748,975 bytes in 8 blocks
==17043==    indirectly lost: 0 bytes in 0 blocks
==17043==      possibly lost: 0 bytes in 0 blocks
==17043==    still reachable: 704 bytes in 2 blocks
==17043==         suppressed: 0 bytes in 0 blocks
==17043== Reachable blocks (those to which a pointer was found) are not shown.
==17043== To see them, rerun with: --leak-check=full --show-reachable=yes
==17043== 
==17043== For counts of detected and suppressed errors, rerun with: -v
==17043== ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 19 from 8)

Sequence.h:

#ifndef SEQUENCE_H
#define SEQUENCE_H
using namespace std;
#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <string>
class GeneSeqs;
class Sequence{
    public:
        Sequence();
        Sequence(char * seqStart,char * aaSeqStart, int length);//Constructor
        ~Sequence();//Destructor -- for the transprof
        //void setup();//Setups up variables.
        char &operator[](int);
        char * getSeq(){return seq;}//returns the address of sequence
        string getAASeq(){return aaSeq;}//returns address of aaSeq
        int getLength(){return seqLen;}
        bool isPositive(){return positiveStrand;}
        /**
         * Setters
         */ 
        void setRevC(char * sequ){revC = sequ;}
        void setSeq(char * sequ){
            seq = sequ;
        }
        void setAASeq(char * a){aaSeq = a;}
        void setLength(int len){seqLen = len;}
        void setAALength(int len){aaSeqLen = len;}
        void setPositivity(bool trufal){
            positiveStrand = trufal;
            if (trufal == false){
                buildRevC();
            }
        }
        //
        int getTCount(){return count.T;}
        int getACount(){return count.A;}
        int getCCount(){return count.C;}
        int getGCount(){return count.G;}
        //void setAASeq(int * a){aaSeq = a;}
        string get_aaSeq(){return aaSeq;}
        bool get_aaSeqExhists(){return aaSeqTranslated;}
        int get_aaSeqLen(){return aaSeqLen;}
        void print();
        void printAA();
        char getAA(int i){return aaSeq[i];}
        int getAALoc(char aa){return AAchar2num(aa);}
        float getMIMScore(){return MIMScore;}
        void setMIM(float MIM){MIMScore = MIM;}
        void translateToAA(int code);
        void computeCrudeCompositions();
        int scoreDegeneracy();
        void degeneracyReport();
        void setName(string n){name = n;}
        float distanceMeasure(Sequence * target);
        bool degeneracyIsScored(){return degenComputed;}
        float distanceMeasure(GeneSeqs * target);
        float distanceMeasureA(GeneSeqs * target, int point, float runningSum);
        int getDegenData(int code);
        void setStartConstant(int i){start = i;}
        void printInfo();
    private:
        struct nucleotideCounts{
            int T;
            int C;
            int A;
            int G;      
        }count;
        struct degeneracyScores{
            int totalCodons;
            int totalCodons_4fold;
            int totalCodons_2fold;
            int total_4fold_A;
            int total_4fold_T;
            int total_4fold_C;
            int total_4fold_G;
            int total_2fold_A;
            int total_2fold_T;
            int total_2fold_G;
            int total_2fold_C;          
        }degScores;
        string name;
        char is4FoldDegenerateSite(int codonIndex);
        char is2FoldDegenerateSite(int codonIndex, char _4FoldResults);
        bool degenComputed;
        float MIMScore;
        void buildRevC();
        string revC;
        char nucleoRevC(char c);
        char * seq;
        int readingFrame;//zero till used...
        int aaSeqLen;
        bool aaSeqTranslated;
        int start;
        int seqLen; //Number of nucleotides in sequence
        bool positiveStrand; //Positive strand = true, negative = false;
        //bool hasY;
        //Genetic sequence in numerical Amino Acide representation;
        string aaSeq;
        void geneSegments2AASeq();
        void computeAATransProf();
        int AAchar2num(char x);
        char AAnum2char(int x);
        char codon2AA(int codonIndex);
        char codon2AA(char * codon);
        char codon2AA(int * codon);
        int Int(string num);    
        int Int(char num);
        char num2nucleo(int a);
        void codonSeq2AASeq();
        int char2nucleo2(char a);
        string String(int num);     
};
#endif
like image 912
DSing Avatar asked Jun 30 '10 17:06

DSing


People also ask

How does segmentation fault occur?

A segfault occurs when a reference to a variable falls outside the segment where that variable resides, or when a write is attempted to a location that is in a read-only segment.

How do you fix a segmentation fault?

It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.

How can segmentation fault be avoided?

Regarding Best practices to avoid segmentation faults, testing the code with tools like Valgrind/Efence helps in catching memory over runs. Besides using tools, writing and organising code carefully helps to great extent.

What causes a segmentation fault in Python?

Tip: A segmentation fault (also known as segfault) is a common condition that causes programs to crash; A segmentation fault is typically caused by a program trying to read from or write to an illegal memory location, that is, part of the memory to which the program is not supposed to have access.


1 Answers

total heap usage: 39,043 allocs, 39,032 frees, 80,022,672,644 bytes allocated

80GB?? I think you have a memory leak somewhere...

I'd vote for the way you're handling "seq" as a char*, vs. using std:string in other places. Looks like an accident waiting to happen.

like image 133
Roddy Avatar answered Oct 15 '22 05:10

Roddy