Skip to content

Commit ca08712

Browse files
casperisfinebyroot
andauthored
Register C global variables to Ruby GC (#1115)
Since Ruby 2.7, C global variables holding references to Ruby objects must be declared to the GC even if they are also held from the Ruby side, otherwise a call to GC.compact might move them to another location. Co-authored-by: Jean Boussier <[email protected]>
1 parent c3d1abc commit ca08712

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

ext/mysql2/client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,7 @@ void init_mysql2_client() {
14531453
mMysql2 = rb_define_module("Mysql2"); Teach RDoc about Mysql2 constant.
14541454
#endif
14551455
cMysql2Client = rb_define_class_under(mMysql2, "Client", rb_cObject);
1456+
rb_global_variable(&cMysql2Client);
14561457

14571458
rb_define_alloc_func(cMysql2Client, allocate);
14581459

ext/mysql2/mysql2_ext.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ VALUE mMysql2, cMysql2Error, cMysql2TimeoutError;
44

55
/* Ruby Extension initializer */
66
void Init_mysql2() {
7-
mMysql2 = rb_define_module("Mysql2");
7+
mMysql2 = rb_define_module("Mysql2");
8+
rb_global_variable(&mMysql2);
9+
810
cMysql2Error = rb_const_get(mMysql2, rb_intern("Error"));
11+
rb_global_variable(&cMysql2Error);
12+
913
cMysql2TimeoutError = rb_const_get(cMysql2Error, rb_intern("TimeoutError"));
14+
rb_global_variable(&cMysql2TimeoutError);
1015

1116
init_mysql2_client();
1217
init_mysql2_result();

ext/mysql2/result.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,9 +1162,13 @@ VALUE rb_mysql_result_to_obj(VALUE client, VALUE encoding, VALUE options, MYSQL_
11621162

11631163
void init_mysql2_result() {
11641164
cDate = rb_const_get(rb_cObject, rb_intern("Date"));
1165+
rb_global_variable(&cDate);
11651166
cDateTime = rb_const_get(rb_cObject, rb_intern("DateTime"));
1167+
rb_global_variable(&cDateTime);
11661168

11671169
cMysql2Result = rb_define_class_under(mMysql2, "Result", rb_cObject);
1170+
rb_global_variable(&cMysql2Result);
1171+
11681172
rb_define_method(cMysql2Result, "each", rb_mysql_result_each, -1);
11691173
rb_define_method(cMysql2Result, "fields", rb_mysql_result_fetch_fields, 0);
11701174
rb_define_method(cMysql2Result, "field_types", rb_mysql_result_fetch_field_types, 0);

ext/mysql2/statement.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,17 @@ static VALUE rb_mysql_stmt_close(VALUE self) {
572572

573573
void init_mysql2_statement() {
574574
cDate = rb_const_get(rb_cObject, rb_intern("Date"));
575+
rb_global_variable(&cDate);
576+
575577
cDateTime = rb_const_get(rb_cObject, rb_intern("DateTime"));
578+
rb_global_variable(&cDateTime);
579+
576580
cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal"));
581+
rb_global_variable(&cBigDecimal);
577582

578583
cMysql2Statement = rb_define_class_under(mMysql2, "Statement", rb_cObject);
584+
rb_global_variable(&cMysql2Statement);
585+
579586
rb_define_method(cMysql2Statement, "param_count", rb_mysql_stmt_param_count, 0);
580587
rb_define_method(cMysql2Statement, "field_count", rb_mysql_stmt_field_count, 0);
581588
rb_define_method(cMysql2Statement, "_execute", rb_mysql_stmt_execute, -1);

0 commit comments

Comments
 (0)