Record pointer creation locations for improved debugging and tracking#815
Record pointer creation locations for improved debugging and tracking#815devjeonghwan wants to merge 2 commits intobytedeco:masterfrom devjeonghwan:master
Conversation
| /** The deallocator associated with this Pointer that should be called on garbage collection. */ | ||
| private Deallocator deallocator = null; | ||
| /** The string where the pointer was created location is recorded. */ | ||
| private String createdLocation = null; |
There was a problem hiding this comment.
It should be enough to have that field only in DeallocatorReference, no?
There was a problem hiding this comment.
Actually, I'm not sure I want that in DeallocatorReference either. It's something that we can do normally with a debugger without adding any overhead to the code:
https://www.jetbrains.com/help/idea/analyze-objects-in-the-jvm-heap.html#-ql1ljx_12
If you can find a way to do this without adding any new fields anywhere except in debug mode obviously, I'd be OK with this, but if there is no way to do this without adding a new field that sticks around even when debug is disabled, then I don't think I'll merge this
There was a problem hiding this comment.
We could do that by adding something like a HashMap<Pointer, String> createdLocations to a utility class like PointerBufferPoolMXBean I guess?
There was a problem hiding this comment.
The Registering, Predeallocating, and Deallocating logs that JavaCPP prints internally output the Pointer object directly, not the DeallocatorReference. Therefore, createdLocation in Pointer is needed for these debug logs to show creation location.
Also, from an application developer's perspective, being able to query the creation location directly from a given Pointer instance is useful for debugging and development.
However, if we modify the log message to output Pointer.deallocator instead of Pointer, and expose a getCreatedLocation() method in Pointer that accesses the DeallocatorReference's createdLocation, then we could remove the createdLocation field from Pointer.
There was a problem hiding this comment.
Ok, Thanks. I'll consider the suggestion you provided.
This PR introduces enhanced debugging capabilities for Pointer object lifecycle tracking.
When the
org.bytedeco.javacpp.logger.debugenvironment variable is set totrue, the system now captures and stores the source code location where Pointer objects are created in bothPointer.createdLocationandDeallocatorReference.createdLocationfields, and includes this information in thetoString()output.This enables developers to easily check exact allocation location in debug logs like
Collecting,Releasing, andRegistering, significantly improving debugging experience and memory leak detection(GC Points) during development.Note that when using JNI's
AllocObjectto create a Pointer and then not calling theJavaCPP_initPointerfunction (Java side,Pointer.init), the created location will be set to null.