Skip to content
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

change malloc/free pairs to use AutoCloseable #21

Open
kortemik opened this issue Dec 8, 2023 · 2 comments
Open

change malloc/free pairs to use AutoCloseable #21

kortemik opened this issue Dec 8, 2023 · 2 comments
Assignees

Comments

@kortemik
Copy link
Member

kortemik commented Dec 8, 2023

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
Copy link
Member Author

kortemik commented Dec 8, 2023

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

@kortemik
Copy link
Member Author

kortemik commented Dec 8, 2023

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

No branches or pull requests

2 participants