-
-
Notifications
You must be signed in to change notification settings - Fork 592
Record pointer creation locations for improved debugging and tracking #815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be enough to have that field only in DeallocatorReference, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.