Fedora Linux Support Community & Resources Center
  #1  
Old 1st April 2011, 10:09 PM
sepoto Offline
Registered User
 
Join Date: Dec 2010
Posts: 174
linuxfedorafirefox
Segmentation Fault (Core Dump)

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.

Code:
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_station_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_station_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;

}
Reply With Quote
  #2  
Old 2nd April 2011, 02:38 PM
bepaald Offline
Registered User
 
Join Date: Nov 2004
Posts: 115
linuxfedorafirefox
Re: Segmentation Fault (Core Dump)

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
Code:
    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:
Code:
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
__________________
Running F14 i686 with KDE
Intel Core 2 Quad 9300 @ 2.5GHz
2G RAM
Geforce 8600GT
Reply With Quote
  #3  
Old 2nd April 2011, 09:42 PM
sepoto Offline
Registered User
 
Join Date: Dec 2010
Posts: 174
windows_7chrome
Re: Segmentation Fault (Core Dump)

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
Reply With Quote
  #4  
Old 2nd April 2011, 09:57 PM
marko's Avatar
marko Online
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,446
linuxfirefox
Re: Segmentation Fault (Core Dump)

There was also a place where you had a loop over b < (int) vbasketballevent2.size();
but then you accessed vector vbasketballevent.
Code:
                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.

Last edited by marko; 3rd April 2011 at 12:46 AM. Reason: typo
Reply With Quote
  #5  
Old 2nd April 2011, 10:22 PM
sepoto Offline
Registered User
 
Join Date: Dec 2010
Posts: 174
linuxfedorafirefox
Re: Segmentation Fault (Core Dump)

HAHAHAHAHA!!!!!! What a silly error on my part!!!!!! That is it!
Reply With Quote
  #6  
Old 3rd April 2011, 12:10 AM
marko's Avatar
marko Online
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,446
linuxfirefox
Re: Segmentation Fault (Core Dump)

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/137...-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:
Code:
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:
Code:
for(int c=0;c<(int)satchan.num_rows();c++) {
	mychans.push_back(string(satchan[c]["cl_tms_chan"]));

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

Last edited by marko; 3rd April 2011 at 01:03 AM. Reason: typo
Reply With Quote
Reply

Tags
core, dump, fault, segmentation

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
yum Segmentation fault (core dumped) jomajo Using Fedora 12 14th December 2010 10:20 PM
BF2 Linux Server 1.2: Segmentation fault (core dumped) iclozm Using Fedora 0 3rd March 2006 05:23 PM
xemacs segmentation fault in fedora core 4 garrett Using Fedora 1 8th July 2005 07:23 PM
APT segmentation fault on fedora core 2 sufianmuneer Installation and Live Media 2 4th July 2004 06:45 PM
APT on FEDORA CORE 1 giving 'Segmentation Fault' prester john Using Fedora 1 4th March 2004 03:02 PM


Current GMT-time: 03:28 (Sunday, 19-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat