Refactor/wasm cstring autoclose#77
Conversation
…ateSyntax Add WasmCStringBuffer (try-with-resources) like WasmInputBuffer for PR73. Removes manual free of filename and export path pointers.
Move WasmCStringBuffer to its own package-private type so v4 can regression-test many throwy iterations against a real WASM instance.
There was a problem hiding this comment.
Hi @krickert , thanks again for your help on this project!
I see the CI is failing, have you had a chance to check that?
While I appreciate the attempt to tighten the story around malloc and free, this PR introduces additional opt-in constructs like WasmCStringBuffer, which increase the overall complexity.
Specifically, the new class is only used in two call sites. At this stage of the project, I think it’s preferable to accept a bit of duplication rather than lock ourselves into a more opinionated pattern.
|
No problem let's just close this one then - I always prefer the auto-closable pattern whenever I see malloc() and free() because it's easy for newbies to cause stupid accidental very very slow memory leaks. But the code works fine and this may have the opposite effect by causing some confusion. And no prob with the contributions - I feel like there's a lot of potential with grpc but also think every java stack needs to tighten up the edges with serialization here. |
|
Thanks for understanding 👍 |
Per #73 I mentioned that if it was approved, I'll go ahead and wrap the other two malloc/free with tighter exception handling.
This pull request refactors how null-terminated UTF-8 strings are allocated and managed in WASM memory within the
ProtobufJava library. The changes introduce a new utility class to handle string memory allocation and deallocation more safely and idiomatically, reducing the risk of memory leaks and improving code clarity. Unit tests are also added to ensure correct resource management.Memory Management Improvements:
WasmCStringBufferclass to encapsulate malloc/free for WASM null-terminated strings, ensuring memory is always freed using try-with-resources andAutoCloseable. (core-common/src/main/java/io/roastedroot/protobuf4j/common/WasmCStringBuffer.java)Protobuf.javato useWasmCStringBuffer, replacing the oldwriteCStringhelper and updating all relevant methods to use try-with-resources for safer memory handling. (core-common/src/main/java/io/roastedroot/protobuf4j/common/Protobuf.java) [1] [2] [3]Testing Enhancements:
WasmCStringBuffercorrectly frees memory even when exceptions occur, preventing WASM memory leaks across iterations. (core-v4/src/test/java/io/roastedroot/protobuf4j/common/WasmCStringBufferTest.java)