Hi,

I am getting the following error in my code.

malloc: *** error for object 0x3b0fe80: double free
*** set a breakpoint in malloc_error_break to debug

When i debug it and do a backtrace i get the following

#0  0x90c994a9 in malloc_error_break ()
#1  0x90c94497 in szone_error ()
#2  0x90bbe463 in szone_free ()
#3  0x90bbe2cd in free ()
#4  0x0000a89d in __gnu_cxx::new_allocator<int>::deallocate (this=0x700048, __p=0x3b0fe80) at ext/new_allocator.h:94
#5  0x0000a8c7 in std::_Vector_base<int, std::allocator<int> >::_M_deallocate (this=0x700048, __p=0x3b0fe80, __n=2) at stl_vector.h:123
#6  0x0000a900 in std::_Vector_base<int, std::allocator<int> >::~_Vector_base (this=0x700048) at stl_vector.h:109
#7  0x0000a95d in std::vector<int, std::allocator<int> >::~vector (this=0x700048) at stl_vector.h:273
#8  0x0000a971 in std::vector<int, std::allocator<int> >::~vector (this=0x700048) at stl_vector.h:273
#9  0x0000a985 in __gnu_cxx::new_allocator<std::vector<int, std::allocator<int> > >::destroy (this=0xbfffe8cf, __p=0x700048) at ext/new_allocator.h:107
#10 0x0000b1ce in std::_Destroy<__gnu_cxx::__normal_iterator<std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >, std::allocator<std::vector<int, std::allocator<int> > > > (__first={_M_current = 0x700048}, __last={_M_current = 0x700060}, __alloc=@0xbfffe8cf) at stl_construct.h:174
#11 0x0000cb1d in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::erase (this=0x8000fc, __first={_M_current = 0x700000}, __last={_M_current = 0x700060}) at vector.tcc:125
#12 0x0000cb90 in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::clear (this=0x8000fc) at stl_vector.h:749
#13 0x00006f1f in circuit::more_illegal (this=0x800000, maxno2=22223, fname=0xbffff710 "../SEQUENTIAL_MITER/s15850.cnf", CONST_INITIAL_STATES=1) at main_program_version_1.cpp:1087

I am not sure is it a memory leak because in my code i am clearing the vectors using clear() function everytime. I wish to know the probable causes for the error and where to look in the code.

Any help is appreciated

Recommended Answers

All 9 Replies

If you can its best to post the code that caused the error.

Hi,

Following is my code.

void circuit::more_illegal(int maxno2, char *fname, int CONST_INITIAL_STATES)
{
	int kf=1;
	std::string ans;
	int prev_kf = CONST_INITIAL_STATES+1;
	more_states =0;
	int ge = rstates[0].size()-1;
	//std::vector<int> vecvar(numflops,2);
	//ofstream outtfile1;
	//outtfile1.open("../SEQUENTIAL_MITER/temp1.txt",ios::out);
	for(int i=0; i<(int)correlated_flops.size(); i++)
	{
		while(kf<=(legal_count[i]))
		{
			// Initializing the vecvar 
			//for(int ha=0; ha<(int)numflops; ha++)
			//vecvar[ha] = 2;
	
			if((kf<=CONST_INITIAL_STATES || kf >= prev_kf) && rstates[kf][ge] == 0) 
			{
				for(int j=0; j<(int)correlated_flops[i].size(); j++)
				{
					for(int k=0; k<((int)rstates[0].size()-1); k++)
					{
						if(correlated_flops[i][j] == rstates[0][k])
						{
							if(rstates[kf][k] == 1)
							{
								rows.push_back((correlated_flops[i][j]+time_frame*numgates));
								rows.push_back(0);	
								vecvar.push_back(rstates[kf][k]);
							}
							if(rstates[kf][k] == 0)
							{
								rows.push_back((correlated_flops[i][j]+time_frame*numgates)*-1);
								rows.push_back(0);
								vecvar.push_back(rstates[kf][k]);
							}
							if(rstates[kf][k] != 1 && rstates[kf][k] != 0)
							{
								cout << "Found error " << "\n";
								exit(-1);
							}
							break;
						}
					}
					columns.push_back(rows);
					rows.clear();
				}
				// Add to the cnf file and check for SAT/UNSAT
				writetofile(maxno2,fname);
				rows.clear();
				columns.clear();
				int g=0;
				ans=call_sat_solver(g,0,fname,1);
				if(ans == "SAT")
				{
						// Delete the clauses added.	
				        delete_last_lines((correlated_flops[i].size()),fname);
						//vecvar.clear();
						//vecvar.resize(0);		
				}

				if(ans == "UNSAT")
				{
						// Add the illegal state to the file 
						more_states++;
						delete_last_lines((correlated_flops[i].size()),fname);
						ofstream outtfile;
						outtfile.open("../SEQUENTIAL_MITER/illegal.txt",ios::app);
				        for(int ty=0; ty<(int)correlated_flops.size(); ty++)
						{
								if(ty!=i)
								{
										for(int tx=0; tx<(int)correlated_flops[ty].size(); tx++)
										{
												outtfile << 2 << " ";
												temporary_illegal_row.push_back(2);
										}
								}
								else
								{
										for(int tr=0; tr<(int)correlated_flops[ty].size(); tr++)
										{
												outtfile << vecvar[tr] << " ";
												//outtfile1 << vecvar[tr] << " ";
												temporary_illegal_row.push_back(vecvar[tr]);
												universal_column[0].push_back(vecvar[tr]);
										}
								}		
							}
					//actual_illegal_states(i,0,0);
					outtfile << "\n";
					temporary_illegal_col.push_back(temporary_illegal_row);
					temporary_illegal_row.clear();
					//outtfile1 << "\n";
					outtfile.close();
					cout << "kf = " << kf << "\n"; 
					rstates[kf][ge] = 1;
				 	//vecvar.clear();
					//vecvar.resize(0);
					universal_column[0].clear();		
				}

				if(ans != "SAT" && ans != "UNSAT")
				{
					//vecvar.clear();
					//vecvar.resize(0);
					cout << "Error found" << "\n";
					exit(-1);	
				}
				vecvar.clear();
			}
			kf++;
		}
		prev_kf = legal_count[i]+1;
		kf = 1;
	}
	//outtfile1.close();
	int ct;
	ofstream outtfile1;
	outtfile1.open("../SEQUENTIAL_MITER/temp1.txt",ios::out);
	for(int rt=0; rt<(int)temporary_illegal_col.size(); rt++)
	{
		for(ct=0; ct<(int)(temporary_illegal_col[rt].size()-1); ct++)
		{
			outtfile1 << temporary_illegal_col[rt][ct] << " ";
		}
		outtfile1 << temporary_illegal_col[rt][ct] << "\n";	
	}	
	outtfile1.close();
	temporary_illegal_row.clear();
	temporary_illegal_col.clear();	
	cout << "-------------------------------------------" << "\n";
	cout << "more_states found = " << more_states << "\n";
	// Correcting the file for illegal states count in file - illegal.txt
	int junky=0;
	std::string line3;
	ofstream inf11("../SEQUENTIAL_MITER/temp.txt",ios::out);	
	ifstream inf12("../SEQUENTIAL_MITER/illegal.txt",ios::in);
	if(!inf12.is_open())
	{
		cerr << "Unable to open file " << "\n";
		exit(-1);
	} 
	inf12 >> junky;
	inf11 << (junky+more_states);
	while(getline(inf12,line3))
	{
		inf11 << line3 << "\n";
	}	
	line3.clear();
	int result_illegal = rename("../SEQUENTIAL_MITER/temp.txt","../SEQUENTIAL_MITER/illegal.txt");
	if(result_illegal==0)
	{
			puts("File successfully renamed \n");
	}
	else
	{
			perror("Error renaming file");
			exit(3);
	}
	inf11.close();
	inf12.close();
}

Is there something wrong in my code.

post the class declaration so that we know how those objects are declared

Following is the class description:

class circuit
{
    // circuit information
	int **output;			// output of the gate
	int *flag;              // flag for every gate 
	int *count_gate;        // count for number of gates at each level
	int *rno;               // count for number of gates at each level
	int num_clauses;
	int or_output;
	std::string answer;
	int jml;
	double comb;
	int number_lines_delete;
	int CONST_INITIAL_STATES;
	int initial_states;	
    int printtestno;
	int *gate_input;		// input of individual gate
	int *input_gates;
	int *output_gates;
	int *flip_gates;
	int time_frame;         // Number of time frames to unroll the circuit
	int **pseudo_outputs;   // Correspondence betwen flip flops and PPO
	int new_states_count;
 	int count_illegal;
	int count_legal;
	int *legal_count;
	int more_states;
	int **line_read;
	int *initial_state;
	
	std::vector<int> vecvar;
	/* Storing the initial 50 reachable states */
	std::vector<int> rstates_row;
	std::vector<vector<int> >rstates;

	/* The group of correlated flip flops obtained by MLP are stored in this vector */
	std::vector<int> correlated_flops_row;
	std::vector<vector<int> >correlated_flops;
	
	/* Storing the AND/OR/NOR clauses*/
	std::vector<int> rows;
	std::vector<vector<int> >columns;
	
	std::vector<int> temporary_illegal_row;
	std::vector<vector<int> > temporary_illegal_col;
	
	std::vector<int> universal_row;
	std::vector<vector<int> > universal_column;

	std::vector<int> truth_table;
	std::vector<vector<int> > truth_table_col;
	
	std::vector<int> temp_vector;

	std::vector<int> get_value;
	// you may wish to add more function declarations (or public variables) here
	public:
	circuit(char *, char *, int );            // constructor
	
	int store_rstates(int, int, int);
	int unroll_circuit(char *, int, int);
	int writetofile(int, char *);
	int delete_last_lines(int ,char *);
	string call_sat_solver(int &, int, char *,int);
	void correct_cnf_file(int, char *);
	void more_illegal(int, char *, int);
};

The only errors I got were unresolved functions (because they were not coded, so that was no surprise) and undeclared variable numgates. Below is what I compiled using VC++ 2008 Express. What compiler are you using?

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class circuit
{
    // circuit information
	int **output;			// output of the gate
	int *flag;              // flag for every gate 
	int *count_gate;        // count for number of gates at each level
	int *rno;               // count for number of gates at each level
	int num_clauses;
	int or_output;
	std::string answer;
	int jml;
	double comb;
	int number_lines_delete;
	int CONST_INITIAL_STATES;
	int initial_states;	
    int printtestno;
	int *gate_input;		// input of individual gate
	int *input_gates;
	int *output_gates;
	int *flip_gates;
	int time_frame;         // Number of time frames to unroll the circuit
	int **pseudo_outputs;   // Correspondence betwen flip flops and PPO
	int new_states_count;
 	int count_illegal;
	int count_legal;
	int *legal_count;
	int more_states;
	int **line_read;
	int *initial_state;
	
	std::vector<int> vecvar;
	/* Storing the initial 50 reachable states */
	std::vector<int> rstates_row;
	std::vector<vector<int> >rstates;

	/* The group of correlated flip flops obtained by MLP are stored in this vector */
	std::vector<int> correlated_flops_row;
	std::vector<vector<int> >correlated_flops;
	
	/* Storing the AND/OR/NOR clauses*/
	std::vector<int> rows;
	std::vector<vector<int> >columns;
	
	std::vector<int> temporary_illegal_row;
	std::vector<vector<int> > temporary_illegal_col;
	
	std::vector<int> universal_row;
	std::vector<vector<int> > universal_column;

	std::vector<int> truth_table;
	std::vector<vector<int> > truth_table_col;
	
	std::vector<int> temp_vector;

	std::vector<int> get_value;
	// you may wish to add more function declarations (or public variables) here
	public:
	circuit(char *, char *, int );            // constructor
	
	int store_rstates(int, int, int);
	int unroll_circuit(char *, int, int);
	int writetofile(int, char *);
	int delete_last_lines(int ,char *);
	string call_sat_solver(int &, int, char *,int);
	void correct_cnf_file(int, char *);
	void more_illegal(int, char *, int);
};

int numgates = 0;

void circuit::more_illegal(int maxno2, char *fname, int CONST_INITIAL_STATES)
{
	int kf=1;
	std::string ans;
	int prev_kf = CONST_INITIAL_STATES+1;
	more_states =0;
	int ge = rstates[0].size()-1;
	//std::vector<int> vecvar(numflops,2);
	//ofstream outtfile1;
	//outtfile1.open("../SEQUENTIAL_MITER/temp1.txt",ios::out);
	for(int i=0; i<(int)correlated_flops.size(); i++)
	{
		while(kf<=(legal_count[i]))
		{
			// Initializing the vecvar 
			//for(int ha=0; ha<(int)numflops; ha++)
			//vecvar[ha] = 2;
	
			if((kf<=CONST_INITIAL_STATES || kf >= prev_kf) && rstates[kf][ge] == 0) 
			{
				for(int j=0; j<(int)correlated_flops[i].size(); j++)
				{
					for(int k=0; k<((int)rstates[0].size()-1); k++)
					{
						if(correlated_flops[i][j] == rstates[0][k])
						{
							if(rstates[kf][k] == 1)
							{
								rows.push_back((correlated_flops[i][j]+time_frame*numgates));
								rows.push_back(0);	
								vecvar.push_back(rstates[kf][k]);
							}
							if(rstates[kf][k] == 0)
							{
								rows.push_back((correlated_flops[i][j]+time_frame*numgates)*-1);
								rows.push_back(0);
								vecvar.push_back(rstates[kf][k]);
							}
							if(rstates[kf][k] != 1 && rstates[kf][k] != 0)
							{
								cout << "Found error " << "\n";
								exit(-1);
							}
							break;
						}
					}
					columns.push_back(rows);
					rows.clear();
				}
				// Add to the cnf file and check for SAT/UNSAT
				writetofile(maxno2,fname);
				rows.clear();
				columns.clear();
				int g=0;
				ans=call_sat_solver(g,0,fname,1);
				if(ans == "SAT")
				{
						// Delete the clauses added.	
				        delete_last_lines((correlated_flops[i].size()),fname);
						//vecvar.clear();
						//vecvar.resize(0);		
				}

				if(ans == "UNSAT")
				{
						// Add the illegal state to the file 
						more_states++;
						delete_last_lines((correlated_flops[i].size()),fname);
						ofstream outtfile;
						outtfile.open("../SEQUENTIAL_MITER/illegal.txt",ios::app);
				        for(int ty=0; ty<(int)correlated_flops.size(); ty++)
						{
								if(ty!=i)
								{
										for(int tx=0; tx<(int)correlated_flops[ty].size(); tx++)
										{
												outtfile << 2 << " ";
												temporary_illegal_row.push_back(2);
										}
								}
								else
								{
										for(int tr=0; tr<(int)correlated_flops[ty].size(); tr++)
										{
												outtfile << vecvar[tr] << " ";
												//outtfile1 << vecvar[tr] << " ";
												temporary_illegal_row.push_back(vecvar[tr]);
												universal_column[0].push_back(vecvar[tr]);
										}
								}		
							}
					//actual_illegal_states(i,0,0);
					outtfile << "\n";
					temporary_illegal_col.push_back(temporary_illegal_row);
					temporary_illegal_row.clear();
					//outtfile1 << "\n";
					outtfile.close();
					cout << "kf = " << kf << "\n"; 
					rstates[kf][ge] = 1;
				 	//vecvar.clear();
					//vecvar.resize(0);
					universal_column[0].clear();		
				}

				if(ans != "SAT" && ans != "UNSAT")
				{
					//vecvar.clear();
					//vecvar.resize(0);
					cout << "Error found" << "\n";
					exit(-1);	
				}
				vecvar.clear();
			}
			kf++;
		}
		prev_kf = legal_count[i]+1;
		kf = 1;
	}
	//outtfile1.close();
	int ct;
	ofstream outtfile1;
	outtfile1.open("../SEQUENTIAL_MITER/temp1.txt",ios::out);
	for(int rt=0; rt<(int)temporary_illegal_col.size(); rt++)
	{
		for(ct=0; ct<(int)(temporary_illegal_col[rt].size()-1); ct++)
		{
			outtfile1 << temporary_illegal_col[rt][ct] << " ";
		}
		outtfile1 << temporary_illegal_col[rt][ct] << "\n";	
	}	
	outtfile1.close();
	temporary_illegal_row.clear();
	temporary_illegal_col.clear();	
	cout << "-------------------------------------------" << "\n";
	cout << "more_states found = " << more_states << "\n";
	// Correcting the file for illegal states count in file - illegal.txt
	int junky=0;
	std::string line3;
	ofstream inf11("../SEQUENTIAL_MITER/temp.txt",ios::out);	
	ifstream inf12("../SEQUENTIAL_MITER/illegal.txt",ios::in);
	if(!inf12.is_open())
	{
		cerr << "Unable to open file " << "\n";
		exit(-1);
	} 
	inf12 >> junky;
	inf11 << (junky+more_states);
	while(getline(inf12,line3))
	{
		inf11 << line3 << "\n";
	}	
	line3.clear();
	int result_illegal = rename("../SEQUENTIAL_MITER/temp.txt","../SEQUENTIAL_MITER/illegal.txt");
	if(result_illegal==0)
	{
			puts("File successfully renamed \n");
	}
	else
	{
			perror("Error renaming file");
			exit(3);
	}
	inf11.close();
	inf12.close();
}

int main()
{
}

i am using g++ compiler. I am sorry about the missing variables and functions. The code is so big that would not be possible to post the complete code. since the backtrace gave error in this function i posted that function.

Oh I see. So you spent hours and hours writing code without bothering to compile it even once :icon_eek: Even experienced programmers will not do what you did.

My suggestion is to start all over again. But this time just write a little bit, compile, correct all errors, the write a little bit more. Repeat that until you are finished.

oh no..i did not mean that....sorry for the confusion. i have written the complete code and compiled it and ran it on many benchmark...in fact the error i get is for bigger benhmarks only.....

..in fact i have rechecked my code many times to make sure there are no errors....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.