@@ -208,12 +208,16 @@ pg_new_result2(PGresult *result, VALUE rb_pgconn)
208208
209209 this = (t_pg_result * )xmalloc (sizeof (* this ) + sizeof (* this -> fnames ) * nfields );
210210 this -> pgresult = result ;
211+ /* Initialize connection and typemap prior to any object allocations,
212+ * to make sure valid objects are marked. */
213+ this -> connection = rb_pgconn ;
214+ this -> typemap = pg_typemap_all_strings ;
215+ this -> p_typemap = RTYPEDDATA_DATA ( this -> typemap );
211216 this -> nfields = -1 ;
212217 this -> tuple_hash = Qnil ;
213218 this -> field_map = Qnil ;
214219 this -> flags = 0 ;
215220 self = TypedData_Wrap_Struct (rb_cPGresult , & pgresult_type , this );
216- RB_OBJ_WRITE (self , & this -> connection , rb_pgconn );
217221
218222 if ( result ){
219223 t_pg_connection * p_conn = pg_get_connection (rb_pgconn );
@@ -227,8 +231,6 @@ pg_new_result2(PGresult *result, VALUE rb_pgconn)
227231 this -> p_typemap = RTYPEDDATA_DATA ( this -> typemap );
228232 this -> flags = p_conn -> flags ;
229233 } else {
230- RB_OBJ_WRITE (self , & this -> typemap , pg_typemap_all_strings );
231- this -> p_typemap = RTYPEDDATA_DATA ( this -> typemap );
232234 this -> enc_idx = rb_locale_encindex ();
233235 }
234236
@@ -1422,8 +1424,9 @@ pgresult_type_map_set(VALUE self, VALUE typemap)
14221424 /* Check type of method param */
14231425 TypedData_Get_Struct (typemap , t_typemap , & pg_typemap_type , p_typemap );
14241426
1425- RB_OBJ_WRITE (self , & this -> typemap , p_typemap -> funcs .fit_to_result ( typemap , self ));
1426- this -> p_typemap = RTYPEDDATA_DATA ( this -> typemap );
1427+ typemap = p_typemap -> funcs .fit_to_result ( typemap , self );
1428+ RB_OBJ_WRITE (self , & this -> typemap , typemap );
1429+ this -> p_typemap = RTYPEDDATA_DATA ( typemap );
14271430
14281431 return typemap ;
14291432}
0 commit comments