Segmentaion Fault

mysql_query give me an error of Segmentation fault. Wat do you think is the problem on my code?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
char* get_file_to_push(const char* cellnum){
	static MYSQL *conn;
	MYSQL_RES *res_set;

	MYSQL_ROW row;
	unsigned long rows;
	char *pChar;
	
	char query[1024];

	conn = mysql_init(NULL);
	if (conn == NULL) {
		wmsg_debug("get_file_to_push -- mysql_init() failed");
		return NULL;
	}
	if (mysql_real_connect(conn, HOSTNAME, USERNAME, PASSWORD, DATABASE, 0, NULL, 0) == NULL) {
		wmsg_debug("get_file_to_push -- mysql_real_connect() failed");
		mysql_close(conn);
		return NULL;
	}else{
		wmsg_debug("get_file_to_push -- connected!");
	}
	
	sprintf(query, "SELECT `A`.`filename`, `A`.`master_contents_id` FROM `master_contents` AS `A` INNER JOIN `push_ques` AS `C` ON `A`.master_contents_id = `C`.master_contents_id WHERE `C`.push = 0 AND `A`.`master_contents_id` NOT IN(SELECT `B`.`master_contents_id` FROM `users_downloads` `B` WHERE `B`.`users_cellnumber`='%s') ORDER BY `C`.`datetime` ASC LIMIT 1", cellnum);
	
	if (mysql_query(conn, query) != 0) {
		wmsg_debug("get_file_to_push:%d -- mysql_query() failed error -- %s", __LINE__, mysql_error(conn));
		mysql_close(conn);
		return NULL;
	} else {
		res_set = mysql_store_result(conn);
		if( res_set == NULL ){
			wmsg_debug("Not enough memory to allocate to res_set. Line:%d", __LINE__);
			return NULL;
		}
		// IF PUSH CONTENT IS ALREADY DOWNLOADED BY USER, SEND LAST CONTENT TO USER
		rows = (unsigned long) mysql_num_rows(res_set);
		if( rows == 0 ){
			mysql_free_result(res_set);

			mysql_ping(conn);
			sprintf(query, "SELECT `A`.`filename`, `A`.`master_contents_id` FROM `master_contents` AS `A` WHERE `A`.`master_contents_id` NOT IN(SELECT `B`.`master_contents_id` FROM `users_downloads` `B` WHERE `B`.`users_cellnumber`='%s') ORDER BY `A`.`master_contents_id` DESC LIMIT 1", cellnum);
			if (mysql_query(conn, query) != 0) {
				wmsg_debug("get_file_to_push -- mysql_query() failed error -- %s", mysql_error(conn));
				mysql_close(conn);
				return NULL;
			}else{
				res_set = mysql_store_result(conn);
				rows = (unsigned long) mysql_num_rows(res_set);
				if( rows <= 0 ) return NULL;
				
				row = mysql_fetch_row(res_set); if( row == NULL ) return NULL;
				pChar = new char[ strlen(row[0])+1 ];
				strcpy(pChar, row[0]);
				mysql_free_result(res_set);res_set=0;
				return pChar;
			}
		}else{
			res_set = mysql_store_result(conn);
			rows = (unsigned long) mysql_num_rows(res_set);
			if( rows <= 0 ) return NULL;
				
			row = mysql_fetch_row(res_set); if( row == NULL ) return NULL;
			pChar = new char[ strlen(row[0])+1 ];
			strcpy(pChar, row[0]);
			mysql_free_result(res_set);res_set=0;
			return pChar;
		}
	}
}
Last edited on
Topic archived. No new replies allowed.