teragrep / jpr_01

Java PCRE Library
Apache License 2.0
5 stars 3 forks source link

change malloc/free pairs to use AutoCloseable #21

Open kortemik opened 11 months ago

kortemik commented 11 months ago

change the Java classes to use AutoCloseable interface so that pointers get automatically freed

see https://www.yegor256.com/2017/08/08/raii-in-java.html

then usage of the resources should happen with try-with-resources which ensures their closing

try (final SomeClass sc = new SomeClass("perhaps arg").open()) { // allocates memory, open returns allocated instance of the class System.out.println("you have call()ed and it provides you with " + sc.something()); } // frees it because close is automatically done

care must be taken that SomeClass methods are blocked while allocation is not done, for example with AtomicBoolean and if not the thr IllegalStateException

see also https://stackoverflow.com/a/69444249

kortemik commented 11 months ago

please notice that SomeClass is mutable and has a state in this sense because it is tracking a mutable state.

kortemik commented 11 months ago

see into https://www.yegor256.com/2023/08/08/two-step-initialization.html as well