Fix #108792: Ensure Metal buffers correctly freed on exit #108940
No reviewers
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
6 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#108940
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "Jason-Fielder/blender:Fix_108792"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Replaces vector of allocations with dynamic linked list.
Bug caused by previously freed buffers still having been in the
list. Linked list enables fast removal of already-released buffers.
Also ensured that the memory manager classes are included in
memory tracking.
Authored by Apple: Michael Parkin-White
I am a bit confused. There was no lists before.
Do you mean that previously it was possible to have situation when some elements of vectors are freed, but not null-ed in the vector or something (leading to attempt to free them again)?
@ -456,0 +516,4 @@
BLI_assert(prev == nullptr);
}
allocations_list_size_--;
BLI_assert(allocations_list_size_ >= 0);
allocations_list_size_
is unsigned, so the check is always true. You can do something likeYes, by "list" I was referring to the original vector of elements. There was an erroneous situation after a recent change introducing memory cleanup which did not nullify the stored element in vector leading to a double free.
Replaced the vector with a linked list to prioritize fast insertion and removal, as the list is only ever iterated upon cleanup during shutdown.
(Will also update or just remove the allocations list size check as it is redundant. A number of the assertions are perhaps a little over-defensive given the usage of the code is rather minimal.)
@ -108,2 +108,4 @@
class MTLBuffer {
public:
gpu::MTLBuffer *next, *prev;
use
ListBase
,ListBaseWrapperTemplate
and the functions inBLI_listbase.h
. Some casts might be needed, but it reduces code-duplication.Thanks, have modified the implementation. Admittedly not 100% sure if there are any catches with ListBase.
I have attempted to utilise both
BLI_freelinkN
for deletion andBLI_freelistN
for complete freeing of the list, but a little unsure how the builtinMEM_freeN
functions work alongside C++ new/delete, and with constructors.i.e. is it valid for gpu::MTLBuffer to be created using
new gpu::MTLBuffer(params...)
, and freed usingMEM_freeN(buffer)
?It appears to be releasing the resources correctly, as it looked like it was sufficient to use
MEM_freeN
based functions, if you haveMEM_CXX_CLASS_ALLOC_FUNCS
specified, however, it would be very helpful if someone could confirm whether the new implementation in the latest commit is correct.Cheers!
@Jason-Fielder, @Sergey, @Jeroen-Bakker, could we please have this fix merged as is, just to avoid those nasty random crashes and let @Michael-Parkin-White-Apple sort out the refactoring later on? 🙏
Update: I ended up building Blender from the main repo and applying the diff patch and as far as I am concerned (#108836) @Michael-Parkin-White-Apple 's fix does solve the crash on exit after a long sculpting session. 👍
Out of an abundance of caution, I have the PR applied and am reporting the following warning on exit:
Thanks for raising this, I'll have a deeper look into this issue and see if there are any problems.
I'm unsure if this over-release is related. Was this also happening without the patch?
I have a suspicion the "Potential double free bug" is actually related to a different bug I was chasing today ( #107500 ), but I never previously saw the warning because of the metal-buffers issue.
... most likely nothing to see here ...
@Michael-Parkin-White-Apple I think you're right and we should revert the change of listbase. Sorry for that. ListBase isn't aware that it needs to call the destructor and could lead to other memory issues.
Ahh right, so this may just free the root ptr but not the destructor?
I think we can keep the list base wrapper for the core list, but perhaps just revert to the manual iteration for release? It also looks like we can use
BLI_remlink
instead ofBLI_freelinkN
to just disconnect a link in the node, then release the memory separately.Apologies, still familiarising with this API.
Best not use the list base api at all. That would only confuse other developers. Just add a note why listbase isn’t used.
To me it looks good now. Thanks for the updates!
Would be cool to have Jeroen's final pass of review.