Prevent temporary mat from being destroyed#45
Merged
Conversation
alanocallaghan
approved these changes
Feb 25, 2026
Contributor
alanocallaghan
left a comment
There was a problem hiding this comment.
Seems somewhat obscene that this would work, but I guess it makes sense that the GC might manage to grab the intermediate object. I'm happy to take this on faith and hope it resolved the issue; at worst it's a neutral change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Running the following Instanseg script on an OME-TIFF image:
is sometimes causing a QuPath crash with a log looking like this:
This happens on Windows 11, with both QuPath 0.6 and QuPath 0.7, and with the corresponding versions of Pytorch and CUDA detailed on https://qupath.readthedocs.io/en/latest/docs/deep/gpu.html.
I managed to get two logs describing the stack trace of the exception:
In short, there is an invalid memory access during a copy from a source buffer to a target buffer. Since the target buffer is created and allocated just before the copy (and the crash may or may not occur with the same exact parameters), I don't think the issue comes from the target buffer, so it must come from the source buffer. The most likely scenario is that the memory of the source buffer is deallocated before the copy is made.
Looking at the stack trace, the source buffer is created on this line:
qupath-extension-djl/src/main/java/qupath/ext/djl/DjlTools.java
Line 407 in 95df7e0
This shows that the buffer is pointing to a memory owned by a temporary
Mat(opencv_dnn.blobFromImage(mat)) created from an existingMatmat. My first thought was thatmatwas deallocated or updated before the copy is made, but I didn't find anything to confirm that. Another idea was that the temporaryopencv_dnn.blobFromImage(mat)was deallocated before the copy, so I made it a non-temporary object (see the changes of this PR). I am not convinced by this, but I never managed to get the crash again with this change, so it might be the solution.