[Bf-blender-cvs] [fdc53012052] asset-browser-grid-view: Fix crash when loading asset library takes multiple redraws

Julian Eisel noreply at git.blender.org
Thu Feb 17 21:42:27 CET 2022


Commit: fdc53012052aa13d7f552def7f24c1f5753823bd
Author: Julian Eisel
Date:   Thu Feb 17 21:39:43 2022 +0100
Branches: asset-browser-grid-view
https://developer.blender.org/rBfdc53012052aa13d7f552def7f24c1f5753823bd

Fix crash when loading asset library takes multiple redraws

===================================================================

M	source/blender/editors/asset/intern/asset_list.cc
M	source/blender/editors/space_assets/asset_view.cc
M	source/blender/editors/space_assets/asset_view.hh

===================================================================

diff --git a/source/blender/editors/asset/intern/asset_list.cc b/source/blender/editors/asset/intern/asset_list.cc
index 433ca7360d6..7dc280c7285 100644
--- a/source/blender/editors/asset/intern/asset_list.cc
+++ b/source/blender/editors/asset/intern/asset_list.cc
@@ -80,8 +80,8 @@ class AssetList : NonCopyable {
   FileListWrapper filelist_;
   /** Storage for asset handles, items are lazy-created on request.
    *  Asset handles are stored as a pointer here, to ensure a consistent memory address (address
-   * inside the map changes as the map changes). */
-  mutable Map<const FileDirEntry *, std::unique_ptr<AssetHandle>> asset_handle_map_;
+   *  inside the map changes as the map changes). */
+  mutable Map<uint32_t, std::unique_ptr<AssetHandle>> asset_handle_map_;
   AssetLibraryReference library_ref_;
 
  public:
@@ -186,8 +186,11 @@ bool AssetList::needsRefetch() const
 
 AssetHandle &AssetList::asset_handle_from_file(const FileDirEntry &file) const
 {
-  return *asset_handle_map_.lookup_or_add(&file,
-                                          std::make_unique<AssetHandle>(AssetHandle{&file}));
+  AssetHandle &asset = *asset_handle_map_.lookup_or_add(
+      file.uid, std::make_unique<AssetHandle>(AssetHandle{&file}));
+  /* The file is recreated while loading, update the pointer here. */
+  asset.file_data = &file;
+  return asset;
 }
 
 void AssetList::iterate(AssetListIterFn fn) const
diff --git a/source/blender/editors/space_assets/asset_view.cc b/source/blender/editors/space_assets/asset_view.cc
index a527e050c4b..30792847baa 100644
--- a/source/blender/editors/space_assets/asset_view.cc
+++ b/source/blender/editors/space_assets/asset_view.cc
@@ -76,20 +76,14 @@ AssetGridViewItem::AssetGridViewItem(const AssetLibraryReference &asset_library_
                                      AssetHandle &asset)
     : ui::PreviewGridItem(ED_asset_handle_get_name(&asset),
                           ED_assetlist_asset_preview_icon_id_request(&asset_library_ref, &asset)),
-      asset_(asset)
+      asset_identifier_(ED_asset_handle_get_identifier(&asset))
 {
 }
 
 bool AssetGridViewItem::matches(const ui::AbstractGridViewItem &other) const
 {
   const AssetGridViewItem &other_item = dynamic_cast<const AssetGridViewItem &>(other);
-  return StringRef(ED_asset_handle_get_identifier(&asset_)) ==
-         StringRef(ED_asset_handle_get_identifier(&other_item.asset_));
-}
-
-AssetHandle &AssetGridViewItem::get_asset()
-{
-  return asset_;
+  return asset_identifier_ == other_item.asset_identifier_;
 }
 
 /* ---------------------------------------------------------------------- */
diff --git a/source/blender/editors/space_assets/asset_view.hh b/source/blender/editors/space_assets/asset_view.hh
index 2ed79cc9053..2e8321ee4de 100644
--- a/source/blender/editors/space_assets/asset_view.hh
+++ b/source/blender/editors/space_assets/asset_view.hh
@@ -53,14 +53,15 @@ class AssetGridView : public blender::ui::AbstractGridView {
 };
 
 class AssetGridViewItem : public ui::PreviewGridItem {
-  AssetHandle &asset_;
+  /* Can't store this here, since the wrapped FileDirEntry will be freed while progressively
+   * loading items. */
+  // AssetHandle &asset_;
+  std::string asset_identifier_;
 
  public:
   AssetGridViewItem(const AssetLibraryReference &asset_library_ref, AssetHandle &);
 
   bool matches(const AbstractGridViewItem &other) const override;
-
-  AssetHandle &get_asset();
 };
 
 void asset_view_create_in_layout(const bContext &C,



More information about the Bf-blender-cvs mailing list