Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@devjeonghwan
Copy link
Member

@devjeonghwan devjeonghwan commented Jul 28, 2025

This PR introduces enhanced debugging capabilities for Pointer object lifecycle tracking.

When the org.bytedeco.javacpp.logger.debug environment variable is set to true, the system now captures and stores the source code location where Pointer objects are created in both Pointer.createdLocation and DeallocatorReference.createdLocation fields, and includes this information in the toString() output.

This enables developers to easily check exact allocation location in debug logs like Collecting, Releasing, and Registering, significantly improving debugging experience and memory leak detection(GC Points) during development.

Note that when using JNI's AllocObject to create a Pointer and then not calling the JavaCPP_initPointer function (Java side, Pointer.init), the created location will be set to null.

/** 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;
Copy link
Member

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?

Copy link
Member

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

Copy link
Member

@saudet saudet Jul 28, 2025

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?

Copy link
Member Author

@devjeonghwan devjeonghwan Jul 28, 2025

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.

Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants