@@ -39,6 +39,7 @@ void dt_grouping_add_to_group(const dt_imgid_t group_id,
3939 dt_grouping_remove_from_group (image_id );
4040
4141 dt_image_t * img = dt_image_cache_get (darktable .image_cache , image_id , 'w' );
42+ if (!img ) return ;
4243 img -> group_id = group_id ;
4344 dt_image_cache_write_release_info (darktable .image_cache , img ,
4445 DT_IMAGE_CACHE_SAFE , "dt_grouping_add_to_group" );
@@ -65,7 +66,7 @@ dt_imgid_t dt_grouping_remove_from_group(const dt_imgid_t image_id)
6566 GList * imgs = NULL ;
6667
6768 const dt_image_t * img = dt_image_cache_get (darktable .image_cache , image_id , 'r' );
68- const int img_group_id = img -> group_id ;
69+ const dt_imgid_t img_group_id = img ? img -> group_id : NO_IMGID ;
6970 dt_image_cache_read_release (darktable .image_cache , img );
7071 if (img_group_id == image_id )
7172 {
@@ -84,10 +85,13 @@ dt_imgid_t dt_grouping_remove_from_group(const dt_imgid_t image_id)
8485 if (!dt_is_valid_imgid (new_group_id ))
8586 new_group_id = other_id ;
8687 dt_image_t * other_img = dt_image_cache_get (darktable .image_cache , other_id , 'w' );
87- other_img -> group_id = new_group_id ;
88- dt_image_cache_write_release_info (darktable .image_cache , other_img ,
88+ if (other_img )
89+ {
90+ other_img -> group_id = new_group_id ;
91+ dt_image_cache_write_release_info (darktable .image_cache , other_img ,
8992 DT_IMAGE_CACHE_SAFE , "dt_grouping_add_to_group" );
90- imgs = g_list_prepend (imgs , GINT_TO_POINTER (other_id ));
93+ imgs = g_list_prepend (imgs , GINT_TO_POINTER (other_id ));
94+ }
9195 }
9296 sqlite3_finalize (stmt );
9397 if (dt_is_valid_imgid (new_group_id ))
@@ -122,13 +126,15 @@ dt_imgid_t dt_grouping_remove_from_group(const dt_imgid_t image_id)
122126 {
123127 // change the group_id for this image.
124128 dt_image_t * wimg = dt_image_cache_get (darktable .image_cache , image_id , 'w' );
125- new_group_id = wimg -> group_id ;
126- wimg -> group_id = image_id ;
127- dt_image_cache_write_release_info (darktable .image_cache , wimg ,
129+ if (wimg )
130+ {
131+ new_group_id = wimg -> group_id ;
132+ wimg -> group_id = image_id ;
133+ dt_image_cache_write_release_info (darktable .image_cache , wimg ,
128134 DT_IMAGE_CACHE_SAFE , "dt_grouping_add_to_group" );
129- imgs = g_list_prepend (imgs , GINT_TO_POINTER (image_id ));
130- // refresh also the group leader which may be alone now
131- imgs = g_list_prepend (imgs , GINT_TO_POINTER (img_group_id ));
135+ imgs = g_list_prepend (imgs , GINT_TO_POINTER (image_id ));
136+ // refresh also the group leader which may be alone now
137+ imgs = g_list_prepend (imgs , GINT_TO_POINTER (img_group_id ));
132138#ifdef USE_LUA
133139 dt_lua_async_call_alien (dt_lua_event_trigger_wrapper ,
134140 0 , NULL , NULL ,
@@ -138,6 +144,7 @@ dt_imgid_t dt_grouping_remove_from_group(const dt_imgid_t image_id)
138144 LUA_ASYNC_TYPENAME , "dt_lua_image_t" , GINT_TO_POINTER (img_group_id ),
139145 LUA_ASYNC_DONE );
140146#endif
147+ }
141148 }
142149 DT_CONTROL_SIGNAL_RAISE (DT_SIGNAL_IMAGE_INFO_CHANGED , imgs );
143150
@@ -150,8 +157,10 @@ dt_imgid_t dt_grouping_change_representative(const dt_imgid_t image_id)
150157 sqlite3_stmt * stmt ;
151158
152159 dt_image_t * img = dt_image_cache_get (darktable .image_cache , image_id , 'r' );
153- const dt_imgid_t group_id = img -> group_id ;
160+ const dt_imgid_t group_id = img ? img -> group_id : NO_IMGID ;
154161 dt_image_cache_read_release (darktable .image_cache , img );
162+ if (!dt_is_valid_imgid (group_id ))
163+ return group_id ;
155164
156165 GList * imgs = NULL ;
157166 DT_DEBUG_SQLITE3_PREPARE_V2 (dt_database_get (darktable .db ), "SELECT id FROM main.images WHERE group_id = ?1" , -1 ,
@@ -161,11 +170,14 @@ dt_imgid_t dt_grouping_change_representative(const dt_imgid_t image_id)
161170 {
162171 const dt_imgid_t other_id = sqlite3_column_int (stmt , 0 );
163172 dt_image_t * other_img = dt_image_cache_get (darktable .image_cache , other_id , 'w' );
164- other_img -> group_id = image_id ;
165- dt_image_cache_write_release_info (darktable .image_cache , other_img ,
173+ if (other_img )
174+ {
175+ other_img -> group_id = image_id ;
176+ dt_image_cache_write_release_info (darktable .image_cache , other_img ,
166177 DT_IMAGE_CACHE_SAFE ,
167178 "dt_grouping_change_representative" );
168- imgs = g_list_prepend (imgs , GINT_TO_POINTER (other_id ));
179+ imgs = g_list_prepend (imgs , GINT_TO_POINTER (other_id ));
180+ }
169181 }
170182 sqlite3_finalize (stmt );
171183 DT_CONTROL_SIGNAL_RAISE (DT_SIGNAL_IMAGE_INFO_CHANGED , imgs );
0 commit comments