@@ -64,27 +64,33 @@ static void rb_mysql_result_mark(void * wrapper) {
64
64
rb_gc_mark (w -> fields );
65
65
rb_gc_mark (w -> rows );
66
66
rb_gc_mark (w -> encoding );
67
+ rb_gc_mark (w -> client );
67
68
}
68
69
}
69
70
70
71
/* this may be called manually or during GC */
71
72
static void rb_mysql_result_free_result (mysql2_result_wrapper * wrapper ) {
72
73
if (wrapper && wrapper -> resultFreed != 1 ) {
74
+ /* FIXME: this may call flush_use_result, which can hit the socket */
73
75
mysql_free_result (wrapper -> result );
74
76
wrapper -> resultFreed = 1 ;
77
+ }
78
+ }
79
+
80
+ /* this is called during GC */
81
+ static void rb_mysql_result_free (void * ptr ) {
82
+ mysql2_result_wrapper * wrapper = ptr ;
83
+ rb_mysql_result_free_result (wrapper );
84
+
85
+ // If the GC gets to client first it will be nil
86
+ if (wrapper -> client != Qnil ) {
75
87
wrapper -> client_wrapper -> refcount -- ;
76
- if (wrapper -> client_wrapper -> refcount == 0 && wrapper -> client_wrapper -> freed ) {
88
+ if (wrapper -> client_wrapper -> refcount == 0 ) {
77
89
xfree (wrapper -> client_wrapper -> client );
78
90
xfree (wrapper -> client_wrapper );
79
91
}
80
92
}
81
- }
82
93
83
- /* this is called during GC */
84
- static void rb_mysql_result_free (void * wrapper ) {
85
- mysql2_result_wrapper * w = wrapper ;
86
- /* FIXME: this may call flush_use_result, which can hit the socket */
87
- rb_mysql_result_free_result (w );
88
94
xfree (wrapper );
89
95
}
90
96
@@ -567,7 +573,7 @@ static VALUE rb_mysql_result_count(VALUE self) {
567
573
}
568
574
569
575
/* Mysql2::Result */
570
- VALUE rb_mysql_result_to_obj (mysql_client_wrapper * client_wrapper , MYSQL_RES * r ) {
576
+ VALUE rb_mysql_result_to_obj (VALUE client , MYSQL_RES * r ) {
571
577
VALUE obj ;
572
578
mysql2_result_wrapper * wrapper ;
573
579
obj = Data_Make_Struct (cMysql2Result , mysql2_result_wrapper , rb_mysql_result_mark , rb_mysql_result_free , wrapper );
@@ -580,8 +586,10 @@ VALUE rb_mysql_result_to_obj(mysql_client_wrapper *client_wrapper, MYSQL_RES *r)
580
586
wrapper -> rows = Qnil ;
581
587
wrapper -> encoding = Qnil ;
582
588
wrapper -> streamingComplete = 0 ;
583
- wrapper -> client_wrapper = client_wrapper ;
589
+ wrapper -> client = client ;
590
+ wrapper -> client_wrapper = DATA_PTR (client );
584
591
wrapper -> client_wrapper -> refcount ++ ;
592
+
585
593
rb_obj_call_init (obj , 0 , NULL );
586
594
return obj ;
587
595
}
0 commit comments