# Thread: Strange Problem of double free corruption

1. ## Strange Problem of double free corruption

Hi, I have encountered a strange problem of double free corruption. Through debugging, I find the problem is at the end of the entropy function, but I cannot figure out why. Thanks for the help.

Code:
```#include<iostream>
#include<iomanip>
#include<string.h>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<map>
using namespace std;

int p1_to_num_MJ(string aa){
map <string,int> AAcode;
AAcode["A"] = 8;
AAcode["C"] = 0;
AAcode["D"] = 14;
AAcode["E"] = 15;
AAcode["F"] = 2;
AAcode["G"] = 9;
AAcode["H"] = 16;
AAcode["I"] = 3;
AAcode["K"] = 18;
AAcode["L"] = 4;
AAcode["M"] = 1;
AAcode["N"] = 12;
AAcode["P"] = 19;
AAcode["Q"] = 13;
AAcode["R"] = 17;
AAcode["S"] = 11;
AAcode["T"] = 10;
AAcode["V"] = 5;
AAcode["W"] = 6;
AAcode["Y"] = 7;
AAcode["X"] = -1;//STOP

//check for valid amino acid
map <string, int> :: const_iterator Iter;
Iter = AAcode.find(aa);
if ( Iter == AAcode.end( ) ){
cerr << "Invalid amino acid: "<< aa << endl;
exit(1);
}

return AAcode[aa]; //returns 0 if aa code is invalid
}

double entropy(const vector<string> &seq)
{
int n = seq.size();
int k = seq[0].length();
vector<double> et(k,0);
int i;
for(i=0;i<k;i++)
{
vector<double> p(20,0);
int j;
for(j=0;j<n;j++)
{
p[p1_to_num_MJ(seq[j].substr(i,1))]++;

}
//cout << "bp0" << endl;
for(j=0;j<20;j++)
{
//cout << j << "\t" << p[j] << endl;
p[j] = p[j]/n;
if(p[j] == 0 || p[j] == 1)
{
}
else
{
et[i] += (-(p[j]*log(p[j])));
}
}

}
//cout << "bp0.5" << endl;
double sum=0;
//cout << "bp1" << endl;
for(i=0;i<k;i++)
{
sum += et[i];
}
//cout << "bp1.5" << endl;
sum /= k;
//cout << "bp2" << endl;
return sum;
}

int main()
{
int node = 1;
int pop = 1000;
int start = 1851;
int end = 3951;

string ofile;
ostringstream ofile_os(ofile);
ofile_os << "seq_entropy_" << node << "_" << pop << "_" << start << "_" << end << ".txt";
ofile = ofile_os.str();
ofstream out(ofile.c_str());

int i;
for(i=start;i<=end;i+=50)
{
cout << "current generation: " << i << endl;
vector< vector<string> > seq;
seq.resize(10);
int j;
string file;
ostringstream file_os(file);
file_os << "RUN1.gen" << setw(10) << setfill('0') << i << ".snap_sex." << setw(3) << setfill('0') << node << ".a";
file = file_os.str();
ifstream in(file.c_str());
if(in.is_open())
{
string line;
while(getline(in,line))
{
//cout << line << endl;
istringstream instr(line);
string field_1;
instr >> field_1;
if(field_1 == "G")
{
int f2,f6,f7;
double f3, f4, f5;
string nseq;
instr >> f2 >> f3 >> f4 >> f5 >> f6 >> f7 >> nseq;
seq[f2].push_back(nseq);

}
}
in.close();
}
else
{
cerr << "file not exist: " << file << endl;
exit(1);
}
out << i << "\t";
//cout << "bp1" << endl;
for(j=0;j<10;j++)
{

cout << "seq_num: " << j << endl;
out << entropy(seq[j]) << "\t";
}
out << endl;
}
out.close();
return 0;
}```

2. Do you have an example file? I'd like to help you debug but I can't find the error without everything you are using.

3. ## example file

The sample file is an ascii file with filename: RUN1.gen0000000851.snap_sex.001.a

The size of the file is 8.5MB and the content is as follows: (The true file has this content repeating 1000 times)

0 0 1000

C 1 1 1.000000e+00 1.000000e+00 9.980151e-01 1.044513e-312 1.000000e+00 5.000000e-01 0.000000e+00 2.121997e-314 10
G 0 1.00 1.00000000 -9.14620000 0 0 THKLILAGNWKMHKTITEAKKYVSLLINALHDVKEFEIVVCPPSTALSEV GGILSVRNIKLGAQNVYYEDKGAYAGEISPLMLQEIGVEYVIVGQSERMR IFKEDDEFISRKVKAVLEKGMTPILCVGETLEEREKGLTFCVVEKQVREG FYGLDKEEAKRVVVAYEPVWAIGTGRVATPPLAQGVHAYIRKLLSEMYDE ETAESIRILYGGSIKPDNYLGLIVQEDIDGGPVGGASLKEAFIELALIMR GV ACTCATAAGCTGATCCTCGCTGGGAACTGGAAGATGCATAAAACGATCAC GGAAGCGAAAAAGTATGTGTCGCTGCTCATTAACGCACTTCACGACGTTA AAGAGTTCGAAATAGTGGTTTGTCCTCCGTCCACAGCTCTATCTGAAGTG GGGGGAATACTCTCTGTTAGAAACATCAAATTGGGAGCTCAAAACGTTTA CTACGAAGACAAGGGAGCGTACGCCGGGGAGATCTCTCCTCTTATGCTGC AAGAGATCGGCGTTGAGTACGTGATCGTGGGACAATCAGAGAGAATGCGT ATTTTCAAAGAAGACGATGAGTTCATAAGTAGGAAAGTCAAAGCGGTGCT TGAGAAAGGTATGACTCCTATTCTCTGCGTTGGAGAAACACTAGAGGAAA GAGAGAAAGGGCTCACTTTCTGCGTTGTGGAAAAACAGGTGAGAGAAGGT TTCTACGGTCTCGACAAAGAGGAAGCAAAGAGAGTGGTAGTAGCTTACGA GCCAGTCTGGGCAATCGGGACAGGAAGGGTGGCGACTCCACCGCTGGCAC AGGGAGTCCATGCGTACATAAGAAAGCTGCTTTCAGAGATGTACGACGAG GAAACAGCGGAATCGATAAGGATTCTCTACGGTGGAAGCATAAAGCCGGA CAATTACCTCGGTCTCATCGTTCAGGAGGATATAGATGGTGGTCCCGTTG GAGGAGCCAGTCTCAAAGAGGCTTTCATAGAACTTGCACTAATAATGAGA GGTGTG
G 1 1.00 1.00000000 -5.36600000 0 0 SSVYKIALGIEYDGSRYYGWEGQNEVRSVQEMLEKALSQVANEPITVSCA GRTDAGVHGTGQVVHFETTALRKDAALTLGVNANLPGDITVRWVQTVPDD FYARFSATARRYRYIIYNQRLRQAVLSKGVTHLYEPLDAERMHRATQCLL GENDFTSFRAEKCQSRTTWRKVMHIKVTRDGPYVVVDIKANAFVLSMVRN IVGSLMEVGAHNQPESWIAELLTAKVTTLAAATASAEGLYLVAVDYPDRY DLPKPTVGPLFLAD TCGTCAGTTTATAAAATTGCGCTGGGCATTGAGTACGACGGCAGTAGGTA TTACGGCTGGGAAGGGCAGAACGAAGTCCGCAGTGTACAGGAGATGCTGG AAAAGGCGCTCTCCCAGGTGGCGAACGAACCCATCACCGTCTCCTGCGCT GGGCGTACTGACGCAGGGGTACACGGTACCGGGCAGGTTGTGCATTTCGA AACTACCGCGCTGCGCAAAGACGCGGCGTTGACCTTGGGCGTAAATGCGA ATTTACCGGGTGACATCACTGTGCGTTGGGTTCAAACTGTACCTGATGAT TTTTATGCCCGATTTAGCGCCACGGCTCGCCGTTATCGCTACATCATCTA CAATCAGCGGCTGCGCCAGGCGGTACTGAGTAAAGGGGTAACCCATCTTT ACGAACCGCTCGACGCTGAACGGATGCATCGGGCTACGCAATGCTTGCTG GGCGAGAATGATTTCACCTCCTTCCGTGCGGAGAAGTGCCAGTCCCGAAC CACGTGGCGCAAAGTTATGCATATTAAAGTCACGCGTGACGGTCCTTATG TGGTGGTAGATATTAAAGCAAATGCTTTTGTTCTTTCTATGGTCAGGAAT ATTGTCGGCAGTCTGATGGAAGTCGGTGCCCACAACCAGCCAGAGAGCTG GATAGCAGAGTTGCTGACGGCAAAGGTCACAACCCTTGCGGCAGCAACGG CAAGTGCGGAAGGGCTGTATCTGGTCGCGGTGGATTACCCTGACCGGTAT GATCTTCCAAAACCGACAGTGGGCCCGCTATTTCTGGCGGAC
G 2 1.00 1.00000000 -4.03940000 0 0 LTWLDVLAPEKQQPFFLNTLQTVANERHSGVPIYPPQKDVFNAFRFAELG DLKVVILGPDPYHGPGQAHGLAFSVRLGIGIPPSLLNMYTLLEKTIPGFT RPDHGYLQSWARQGVLLLNTVLTVRAVQAHSHASLGWETFTDKVISLINQ HREGVVFLLWGSHAQKKGAILDKQRHHVPKAPHPSPLTAHGGFFGCNHFV LANKWLEQHGETPIDWIPVLPAESE TTAACCTGGCTTGACGTGCTGGCTCCAGAGAAGCAGCAACCCTTCTTTCT TAATACCCTTCAGACCGTCGCCAACGAGCGGCATTCCGGCGTCCCTATCT ACCCACCACAAAAAGATGTCTTTAACGCGTTCCGATTTGCAGAGTTGGGT GACCTTAAGGTGGTGATTCTCGGCCCGGATCCTTATCACGGACCGGGACA GGCGCACGGTCTTGCATTTTCCGTTCGTCTCGGCATTGGCATTCCTCCAT CATTATTGAATATGTATACACTGCTGGAAAAAACTATTCCGGGCTTCACC CGCCCTGATCATGGTTATCTTCAAAGCTGGGCGCGTCAGGGCGTTCTGCT ACTCAATACTGTGTTGACGGTACGCGCAGTACAGGCGCATTCTCACGCCA GCCTCGGCTGGGAAACCTTCACCGATAAAGTGATCAGCCTGATTAACCAA CATCGCGAAGGCGTGGTGTTTTTGTTGTGGGGATCGCATGCGCAAAAGAA AGGGGCGATTCTAGATAAGCAACGCCATCATGTACCGAAAGCACCGCATC CGTCGCCGCTTACGGCGCATGGTGGATTCTTTGGCTGCAACCATTTTGTG CTCGCAAATAAGTGGCTGGAGCAACATGGCGAGACGCCGATTGACTGGAT TCCAGTACTACCGGCTGAGAGTGAG
G 3 1.00 1.00000000 -8.11470000 0 0 ASTADIISQLKKLSLAESAVAKDSHPDVNIVDLMRNYISQQLSKISGVDS SLIFAALEWTNNMESGDLLIPIPRLKIKSANPKDLAVQWSEKFPCGDFLE KVVANGTLIQFFFNPQFLAKLVIPAILTRKEGYG GCTAGCACAGCAGATATAATTTCTCAACTAAAGAAACTATCTCTCGCGGA ATCTGCGGTTGCCAAGGATTCTCATCCTGATGTAAATATCGTGGATTTGA TGAGAAATTACATTTCACAACAATTAAGCAAGATTTCCGGCGTTGACTCG TCTCTTATTTTTGCAGCATTAGAATGGACAAATAATATGGAGAGCGGCGA TTTGTTGATTCCTATCCCAAGATTGAAAATCAAGAGCGCCAATCCAAAGG ATTTGGCAGTCCAATGGTCTGAAAAATTCCCATGTGGAGATTTCTTGGAG AAAGTTGTAGCGAATGGCACGCTCATCCAGTTTTTCTTCAACCCTCAATT TTTAGCAAAACTTGTCATCCCAGCTATCTTAACCAGAAAGGAGGGTTATG GT
G 4 1.00 1.00000000 -12.07210000 0 0 SCKLVINQKVIIEFSSPNIAKPFHDGHLRSTIIGALLANLYEKLGWEVIR INYLGDWGKQFGLLAVGFERYGNEEALVKEPIQHLFDVYVRINKDIEEEG DSIQLERSTNGKAREYFKRMEEGDEEALKIWKRFLEYSIEKYIDTYARLN IKFAVYSGESQVSKESMVKAMELFKEKGLTHEDKGAVLIDLIKFDKKLGK PFVQKSDGTTLYLTRDLGAAMDRYEKYHFDKMIYVIASVQDLHAAQFFEI LKQMGFEWAKYLQHVNFGMVQGMSARNGTVVFLVIILEETKAKMHEVMKK NETKYAQIEHPEEVADLVGISALMTQDMQGKRINNYEFKWERMLSFEG TCTTGCAAATTGGTTATAAACCAGAAAGTTATTATTGAATTCTCCTCTCC CAACATTGCCAAGCCGTTTCATGATGGTCATTTAAGATCCACTATTATTG GTGCCTTATTGGCCAACCTTTATGAAAAATTAGGTTGGGAGGTTATCAGA ATTAACTACCTGGGTGACTGGGGTAAACAATTTGGTCTTTTAGCTGTTGG TTTTGAACGATACGGTAATGAAGAGGCTTTAGTTAAGGAACCAATTCAGC ACCTGTTCGATGTTTACGTTCGTATCAACAAGGATATTGAAGAAGAAGGT GACAGTATTCAATTAGAACGATCCACCAATGGAAAAGCTCGTGAATATTT CAAGAGAATGGAAGAGGGTGACGAGGAAGCTTTAAAAATTTGGAAGAGAT TCCTTGAATATTCTATTGAAAAATATATTGACACATATGCACGTTTGAAC ATCAAATTTGCTGTTTACTCGGGTGAATCTCAAGTTTCTAAAGAATCCAT GGTCAAAGCGATGGAGCTGTTCAAGGAAAAGGGCTTAACACATGAGGATA AGGGTGCAGTTTTGATCGATTTGATAAAATTTGACAAAAAGTTAGGAAAG CCTTTTGTTCAAAAATCGGACGGTACTACGTTATATCTAACAAGAGACCT TGGTGCAGCTATGGATCGTTATGAAAAGTATCATTTTGACAAGATGATTT ACGTTATCGCTTCTGTACAAGATTTGCACGCTGCCCAATTCTTCGAAATT TTGAAGCAGATGGGCTTTGAGTGGGCTAAATATCTACAGCATGTCAACTT TGGTATGGTCCAAGGTATGTCAGCAAGAAACGGTACTGTTGTCTTCTTAG TTATTATCTTGGAGGAAACTAAGGCAAAAATGCACGAAGTTATGAAAAAG AATGAGACCAAATACGCGCAAATTGAGCATCCAGAAGAAGTTGCCGATTT AGTTGGTATCTCTGCCCTCATGACTCAAGATATGCAAGGTAAGCGTATCA ACAACTACGAGTTTAAATGGGAAAGAATGCTTTCATTCGAAGGT
G 5 1.00 1.00000000 -9.20620000 0 0 ETGPYLQFAHSRLRSVERNASGITQEKWKNADFSLLKEPAAKLLIRLLGQ YPDVLSNAIKTHEPTTLVTYLLKLTEQVSSCDDVLRVAGQTEELATARLA LYGAARQVLYNGMRLLGLPPLERM GAAACCGGTCCATACCTTCAATTTGCTCACTCAAGGTTAAGGTCAGTTGA AAGAAATGCTTCTGGTATCACCCAAGAAAAATGGAAAAATGCCGATTTTT CGTTATTAAAAGAACCTGCCGCAAAACTGTTGATTAGACTGCTAGGCCAA TACCCTGATGTTTTGAGCAATGCAATTAAAACCCACGAGCCAACAACCCT GGTCACATATTTATTGAAATTGACTGAACAGGTGTCTTCTTGTGATGACG TCTTAAGGGTTGCTGGTCAAACTGAAGAATTAGCAACTGCCCGTCTGGCT CTATATGGTGCTGCAAGACAAGTTTTATACAACGGTATGCGCTTGTTAGG TCTACCTCCCTTAGAAAGAATG
G 6 1.00 1.00000000 -8.83090000 0 0 TIISTKYLLQDAQSNGYAVPAFSIHNAKTIQAILEVCSEMRSPVILAGTP GTFKHIPLEEIYALCSAYSTTHNLPLALHLDNHESLDDIRRKVHAGVRSA MSDASHFPFAENVKVVKSVVDFCQSQECSVEGQLGRLGGVEDDISVDAES AFLTDPQGAKRYVELTGVDSLAVAIGTALGLYSKTPKIDFQRLAEIREVV EVPLVLHGASDVPDEFVRRTIELGVTKVNVAIELKIAFAGAVKAWFAENP QGNDPRSYMRVGMDAMQEDVRNKINVCGSANRIP ACCATTATCTCCACTAAATATCTGTTACAGGACGCCCAGTCCAATGGCTA CGCGGTGCCTGCTTTTAGCATTCATAACGCCAAGACGATCCAAGCGATCC TCGAAGTGTGCAGTGAAATGCGATCGCCGGTGATCCTCGCCGGAACGCCG GGGACCTTTAAACACATCCCGCTGGAAGAGATCTACGCCCTGTGTAGCGC CTATTCCACAACACACAACCTGCCACTGGCGCTGCATCTCGACAACCACG AATCGCTGGATGATATTCGCCGTAAAGTCCACGCAGGTGTGCGCAGTGCG ATGAGCGACGCCAGCCACTTCCCGTTTGCCGAGAACGTGAAGGTGGTGAA ATCGGTTGTTGACTTCTGCCAATCACAAGAGTGCAGCGTGGAAGGACAAC TGGGCCGTCTGGGCGGTGTAGAAGATGACATTAGCGTTGACGCCGAAAGT GCATTCCTGACCGATCCACAAGGAGCTAAACGCTATGTCGAACTGACTGG GGTCGACAGCCTGGCGGTAGCGATTGGTACCGCGCTCGGCTTATACAGCA AAACGCCGAAGATTGATTTCCAGCGGCTGGCGGAAATTCGTGAAGTGGTG GAAGTTCCTCTGGTGCTGCATGGTGCCAGCGATGTTCCGGATGAATTTGT CCGTCGCACCATTGAACTTGGCGTCACAAAAGTGAACGTTGCCATAGAAT TAAAAATAGCCTTCGCTGGCGCGGTTAAAGCCTGGTTTGCGGAAAATCCG CAGGGTAATGATCCTCGTTCTTATATGCGCGTCGGAATGGATGCAATGCA AGAAGATGTGAGAAATAAAATTAATGTTTGTGGTTCAGCCAATCGAATTC CA
G 7 1.00 1.00000000 -4.44930000 0 0 RMRKGIILAGGSSTRLYPVTSAVSKQLLPIYDKPMIYYPLSTLMLARIRD ILIISTAQDTPRFQQMLGDGSQWGLNLQYKEQASPDGLAQAFIVGEVFIG GDDCALVLGDNIFYGLDLPKLMKDAVNKESGSTVFVYHVNNPERYGVVEF DKNGTGISLEEKPLEPKSNYGVTGLYFYDNDVVQMAKSLKPSARGELEIT DINRIYLEQGRLTVAMMGRRYAWLDTGTHQSLIEASDFFATIEDRQGLNV SCPEEIAFRKDFIDVEQVRKLAITLRKNNYGQYLYKMTNN AGAATGCGTAAAGGTATTATTTTAGCGGGTGGTTCTAGTACACGTCTTTA TCCTGTGACTAGCGCTGTCAGTAAACAGCTATTACCTATTTATGATAAAC CGATGATCTATTACCCGCTCTCTACACTGATGTTGGCGCGTATTCGCGAT ATTTTGATTATCAGTACAGCTCAGGATACTCCTCGTTTTCAACAAATGCT AGGTGACGGTAGCCAGTGGGGCCTGAATCTTCAGTACAAAGAGCAAGCGA GCCCAGATGGCCTAGCGCAGGCATTTATCGTCGGAGAAGTGTTTATTGGT GGTGATGATTGTGCTTTGGTTCTTGGTGATAATATCTTTTACGGTCTCGA TCTGCCGAAGCTAATGAAGGACGCAGTTAACAAAGAAAGTGGTTCAACGG TATTTGTCTATCACGTTAATAATCCAGAACGCTATGGTGTCGTTGAGTTT GATAAAAACGGTACGGGAATCAGTCTGGAAGAAAAACCGTTAGAACCAAA GAGTAATTACGGCGTTACAGGTCTGTACTTTTATGATAACGACGTAGTAC AGATGGCGAAAAGCTTGAAGCCGTCTGCACGTGGTGAGTTAGAAATTACA GATATTAACCGTATTTATCTTGAGCAGGGACGTCTAACTGTCGCGATGAT GGGGCGTCGCTACGCGTGGCTGGACACGGGGACTCATCAGAGTCTGATAG AAGCAAGTGATTTTTTTGCGACAATTGAAGACCGCCAGGGATTGAATGTT TCCTGTCCTGAAGAGATTGCATTTCGTAAAGATTTTATTGATGTTGAGCA AGTAAGGAAATTAGCTATAACACTAAGAAAGAATAATTATGGGCAGTATC TTTATAAAATGACGAATAAT
G 8 1.00 1.00000000 -5.96730000 0 0 MNIGLLEALDQLDEEKGISKEEVIPILEKALVSAYSKNFGNSNNVEVVID RNTGNIKVYQLLEVVEEVEDTDTQISLEEAKKIDPLAKVGSIVKKELHVM NFGRIAAQTAKQVLIQRSRELEKKKQ ATGAACATAGGATTGCTGGAAGCCCTCGACCAGCTGGATGAAGAAAAAGG AATTTCCAAAGAAGAGGTCATTCCTATCCTTGAAAAAGCACTGGTGAGCG CTTACAGCAAGAACTTTGGAAATTCCAACAACGTGGAGGTCGTTATAGAT AGGAACACGGGAAACATAAAAGTGTATCAGCTCCTCGAAGTTGTGGAAGA AGTGGAAGATACAGATACACAGATATCTCTCGAGGAGGCCAAAAAGATCG ACCCCCTCGCGAAAGTTGGGTCTATTGTGAAGAAGGAACTCCACGTTATG AATTTTGGAAGAATAGCCGCGCAGACGGCGAAGCAGGTTCTCATTCAGAG AAGCAGAGAACTCGAGAAGAAAAAACAG
G 9 1.00 1.00000000 -5.07060000 0 0 FEKDSELQGTVTTAEVIRVMGEWADIRIGKLETRLPKKEWIPGEEIKPDD VVKVYIIDVVKTTKGPKILVRR TTCGAGAAGGATTCCGAACTCCAAGGAACGGTTACAACCGCTGAAGTCAT AAGAGTCATGGGAGAGTGGGCAGACATCAGAATAGGAAAGCTCGAGACAA GGCTTCCAAAGAAAGAGTGGATTCCCGGGGAGGAAATCAAACCCGATGAT GTGGTGAAGGTCTACATCATCGATGTGGTTAAAACAACCAAGGGGCCGAA GATACTCGTGCGCAGG

Originally Posted by whiteflags
Do you have an example file? I'd like to help you debug but I can't find the error without everything you are using.

4. OK, I was not able to reproduce your problem, sadly. Had there been heap corruption on my end my debugger would have screamed at me. Didn't happen.

As I was watching the program execute I saw a couple things that worried me.

Code:
```        //cout << "bp0" << endl;
for(j=0; j<20; j++)
{
//cout << j << "\t" << p[j] << endl;
p[j] = p[j]/n;

if(p[j] == 0 || p[j] == 1)
{
}

else
{
et[i] += (-(p[j]*log(p[j])));
}
}```
You may recognize this - it's part of the entropy function starting on line 65. I spent too much time of the program's run in the colored area of the code. Something about that doesn't feel right.

I suspect that the input was not correct. You ought to make a contrived input file, something I can reliably test, basically, and attach a zip file here so that I can produce your error.

In any case, I'm attaching my results too.

5. The symptom you describe is a classic hint that somewhere in your code an attempt is made to go outside bounds of a container. For example, accessing container[10] when container only contains 10 elements.

Bear in mind that, when a program crashes, the culprit can be in any code executed before that point. Crashes are not necessarily immediate.

You're using lots of ints as indices into vectors. That's not wrong, but only if those integral values are valid indices. You perform very few checks that the indices are valid (eg if a vector has size() 10, then 10 is an an invalid index).

Depending on content of your file (which is not exactly a clean and intuitive format) there are many possibilities for your code to access vector/set/string elements using an invalid indices.

I therefore suggest a first step to finding your problem is to check every index you can. For example, make sure that an index into a vector is between 0 and that_vector.size() - 1, before accessing the element.

It would probably help if, when reading the file, you perform bounds checking where possible on the input, rather than just bundling data into vectors or strings.

6. Along the same lines as grumpy's comment; line 60 looks a bit suspect to me:
Code:
`  p[p1_to_num_MJ(seq[j].substr(i,1))]++;`
Does seq[j].substr(i,1) ever equal "X"?, 'cos if it does then splitting that line up into its constituent parts...
Code:
```  seq[j].substr(i,1) == "X"
p1_to_num_MJ( "X" ) == -1
p[-1] == Oh Dear!```