Skip to content

Commit

Permalink
Return early when unbinding devide memory, avoiding a memory leak (#2341
Browse files Browse the repository at this point in the history
)
  • Loading branch information
warmenhoven committed Sep 24, 2024
1 parent 1c0b0f2 commit 2bf3add
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions MoltenVK/MoltenVK/GPUObjects/MVKImage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,10 @@
if (_deviceMemory) { _deviceMemory->removeImageMemoryBinding(this); }
MVKResource::bindDeviceMemory(mvkMem, memOffset);

if (!_deviceMemory) { return VK_SUCCESS; }

auto& mtlFeats = getMetalFeatures();
bool usesTexelBuffer = mtlFeats.texelBuffers && _deviceMemory; // Texel buffers available
bool usesTexelBuffer = mtlFeats.texelBuffers; // Texel buffers available
usesTexelBuffer = usesTexelBuffer && (isMemoryHostAccessible() || mtlFeats.placementHeaps) && _image->_isLinear && !_image->getIsCompressed(); // Applicable memory layout

// macOS before 10.15.5 cannot use shared memory for texel buffers.
Expand All @@ -425,7 +427,8 @@
_mtlTexelBufferOffset = getDeviceMemoryOffset();
} else {
// Create our own buffer for this.
if (_deviceMemory && _deviceMemory->_mtlHeap && _image->getMTLStorageMode() == _deviceMemory->_mtlStorageMode) {
if (_ownsTexelBuffer) { [_mtlTexelBuffer release]; }
if (_deviceMemory->_mtlHeap && _image->getMTLStorageMode() == _deviceMemory->_mtlStorageMode) {
_mtlTexelBuffer = [_deviceMemory->_mtlHeap newBufferWithLength: _byteCount options: _deviceMemory->getMTLResourceOptions() offset: getDeviceMemoryOffset()];
if (_image->_isAliasable) { [_mtlTexelBuffer makeAliasable]; }
} else {
Expand All @@ -443,7 +446,7 @@
}

flushToDevice(getDeviceMemoryOffset(), getByteCount());
return _deviceMemory ? _deviceMemory->addImageMemoryBinding(this) : VK_SUCCESS;
return _deviceMemory->addImageMemoryBinding(this);
}

void MVKImageMemoryBinding::applyMemoryBarrier(MVKPipelineBarrier& barrier,
Expand Down

0 comments on commit 2bf3add

Please sign in to comment.