PDA

View Full Version : Segmentation Fault (Core Dump)


sepoto
1st April 2011, 10:09 PM
I know that a segmentation fault normally occurs when I try to access a vector's address that does not exist or is outside of it's bounds. I looked at my code and I do not see anywhere where this might be occuring. This segmentation fault does not occur every time I run my program. It only occurs infrequently which is making it all that much harder to de bug it. I would post my code to the mysqlpp mailing list but they always flame me and tell me to go read a book on C++. Maybe someone out there has a more trained eye than I do and can spot quickly what the problem is. If not I will keep working on it. Thank all of you for your time and patience.


vector< vector<string> > dig(string ssport) {



Query qbasketballlineup = conn.query();

qbasketballlineup << "SELECT tf_database_key, tf_reduced_titlec, tf_reduced_descc, tf_genre_desc138, tf_epi_title, tf_game_date FROM sports_program where tf_genre_desc138 like '%"<< ssport << "%' and tf_genre_desc137='Sports event' and tf_game_date between '" + yestdate + "' and '" + tomdate + "' order by tf_reduced_titlec;";

vector<mysqlpp::Row> vbasketballlineup;

qbasketballlineup.storein(vbasketballlineup);



vector< vector<string> > basketball;



for(int a=0;a<(int)vbasketballlineup.size();a++) {



vector<string> mychans;

vector<int> vair_time;



Query qbasketballevent = conn.query();

qbasketballevent << "select distinct tf_live_tape_delay,tf_air_date,tf_air_time,tf_stat ion_num,tf_database_key,tf_hdtv from sports_schedule where tf_database_key='" <<string(vbasketballlineup[a]["tf_database_key"])<<"' and tf_live_tape_delay='Live' and tf_hdtv='N'";

vector<mysqlpp::Row> vbasketballevent;

qbasketballevent.storein(vbasketballevent);



for(int b=0;b<(int)vbasketballevent.size();b++) {

int air_time = atoi(vbasketballevent[b]["tf_air_time"]);

string air_date = string(vbasketballevent[b]["tf_air_date"]);

air_time = cnvrtime(air_time,string(mytz[0]["TZ"]),tm_isdst);

if(air_time<0) { air_time = 2400 + air_time; air_date = sub1day(air_date); }

cout << air_date << "," << air_time << "," << ssport << endl;



if(air_date == todaysdate) {

vair_time.push_back(air_time);



Query queryh = conn.query();

queryh<<"select * from sat_lineup_record where cl_headend_id='"<<string(vmysathead[0])<<"' and cl_station_num='"<<string(vbasketballevent[b]["tf_station_num"])<<"'";

StoreQueryResult satchan = queryh.store();

if(satchan.num_rows()>0) {

for(int c=0;c<(int)satchan.num_rows();c++) {

string temp = string(satchan[c]["cl_tms_chan"]);

temp = temp;

mychans.push_back(string(temp));

cout << temp << endl;

}

}//if satchan

}//todaysdate

}//b for ss



Query qbasketballevent2 = conn.query();

qbasketballevent2 << "select distinct tf_live_tape_delay,tf_air_date,tf_air_time,tf_stat ion_num,tf_database_key,tf_hdtv from sports_schedule where tf_database_key='" <<string(vbasketballlineup[a]["tf_database_key"])<<"' and tf_live_tape_delay='Live' and tf_hdtv='Y'";

vector<mysqlpp::Row> vbasketballevent2;

qbasketballevent2.storein(vbasketballevent2);



for(int b=0;b<(int)vbasketballevent2.size();b++) {

int air_time = atoi(vbasketballevent2[b]["tf_air_time"]);

string air_date = string(vbasketballevent2[b]["tf_air_date"]);

air_time = cnvrtime(air_time,string(mytz[0]["TZ"]),tm_isdst);

if(air_time<0) { air_time = 2400 + air_time; air_date = sub1day(air_date); }

cout << air_date << "," << air_time << "," << ssport << endl;



if(air_date == todaysdate) {

vair_time.push_back(air_time);



Query queryh = conn.query();

queryh<<"select * from sat_lineup_record where cl_headend_id='"<<string(vmysathead[0])<<"' and cl_station_num='"<<string(vbasketballevent[b]["tf_station_num"])<<"'";

StoreQueryResult satchan = queryh.store();

if(satchan.num_rows()>0) {

for(int c=0;c<(int)satchan.num_rows();c++) {

string temp = string(satchan[c]["cl_tms_chan"]);

temp = temp + "HD";

mychans.push_back(string(temp));

cout << temp << endl;

}

}//if satchan





}//todaysdate

}//b for ss



sort(mychans.begin(),mychans.end(),myLessThan());

sort(vair_time.begin(),vair_time.end());

for(int b=0;b<(int)mychans.size();b++) { cout << mychans[b] << endl; }



for(int c=0;c<(int)mychans.size();c++) {

for(int d=c+1;d<(int)mychans.size();d++) {

if(mychans[c]==mychans[d]) { mychans.erase(mychans.begin()+d); }

}

}



if(mychans.size()>0) {

//cout << vair_time[0] << "," << vbasketballlineup[a]["tf_reduced_titlec"] << "," << vbasketballlineup[a]["tf_epi_title"];

vector<string> temp;

stringstream sstemp;

sstemp << vair_time[0];

temp.push_back(sstemp.str());

temp.push_back(string(vbasketballlineup[a]["tf_reduced_titlec"]));

temp.push_back(string(vbasketballlineup[a]["tf_epi_title"]));

string stemp;

for(int b=0;b<(int)mychans.size();b++) {

stemp = stemp + string(mychans[b]) + ", ";

}

temp.push_back(stemp);



basketball.push_back(temp);

}

}



return basketball;

}

bepaald
2nd April 2011, 02:38 PM
Well I don't see anything obviously wrong with it, but it is kind of hard to read this code. Also, it would be a huge help if you could provide some code that actually compiles (including myLessThan() and a main function that calls dig()). Any chance you could do that?

Alternatively, you could try to compile the code with the -g switch and run the program through valgrind (it's in the repo), it could tell you where in the program the segfault occurs.

On a seperate note, this bit of code looks a bit weird to me
for(int c=0;c<(int)mychans.size();c++) {
for(int d=c+1;d<(int)mychans.size();d++) {
if(mychans[c]==mychans[d]) { mychans.erase(mychans.begin()+d); }
}
}

Do you intend to remove duplicates from the vector? If that is the case, this loop won't do it if a value occurs more than twice (if something appears in the vector 3 times, only one will be erased). If that is not the intended behaviour, consider removing that loop and replacing it with:
vector<string>::iterator it = unique(mychans.begin(), mychans.end());
mychans.resize(it - mychans.begin());
It'll be more efficient to not call erase() multiple times anyway.

bepaald

sepoto
2nd April 2011, 09:42 PM

Wow what a great idea. I have to admit I was thinking about that loop last night and was wondering if perhaps the segmentation fault is being caused by it. I am going to replace with your code and run the program again a few times to begin with. Thank you so much. That is very kind of you to share that with me.

---------- Post added at 08:42 PM ---------- Previous post was at 06:56 PM ----------

I have traced the problem down to this exact line as it sits in hdig.h

Query queryh = conn.query(); <-- causes segmentation fault

marko
2nd April 2011, 09:57 PM
There was also a place where you had a loop over b < (int) vbasketballevent2.size();
but then you accessed vector vbasketballevent.

queryh << "select * from sat_lineup_record where cl_headend_id='"
<< string(vmysathead[0]) << "' and cl_station_num='"
<< string( vbasketballevent[b]["tf_station_num"]) << "'";

If there's a chance vbasketballevent is smaller then it could segment fault.

sepoto
2nd April 2011, 10:22 PM
HAHAHAHAHA!!!!!! What a silly error on my part!!!!!! That is it!

marko
3rd April 2011, 12:10 AM
Some more tips,

1)
In STL it's considered better to use the container.empty() function instead of container.size() > 0
that is:

if (!sportschannels.empty()) {
// do some processing
}

is considered better than

if (sportschannels.size() > 0) {
// processing
}
due to being a bit more readable, shorter, also in the STL standard, empty() is supposed to be constant time but in some implementations size() might be linear time.

http://bytes.com/topic/c/answers/137416-effective-stl-item-4-size-vs-empty

As you noticed with the vbasketballevent and vbasketballevent2 misuse, it's very easy to confuse two variables with nearly identical, long names (ex: two very long names just differentiated by the final digit or character). It would be better to make variable names more different. It also seems likely that your dig() method is way too complex and should be broken up into multiple methods if you can.

There was also a few odd places where the code was doing redundant things:
for(int c=0;c<(int)satchan.num_rows();c++) {
string temp = string(satchan[c]["cl_tms_chan"]);
temp = temp; << ??? doesn't do anything, probably left in by accident
mychans.push_back(string(temp));

cout << temp << endl;
}

can be shortened to this:

for(int c=0;c<(int)satchan.num_rows();c++) {
mychans.push_back(string(satchan[c]["cl_tms_chan"]));

cout << mychans.back() << endl;
}