Closed ivan-pi closed 2 years ago
Actually, the nlopt_opt
will destroy the handle automatically using a final
procedure:
The destroy
procedure is additionally exposed to allow the user to perform this operation manually, since the procedure will check whether the handle is actually associated:
The rationale for having both is that it can be inconvenient to not have a way to manually destroy an object and require it to go out of scope for proper finalization (either by using block
or by assignment).
Right, I missed that, sorry. I was only looking at the example in the readme.
I fully agree with your reasoning.
Maybe we could update the documentation for this and add a code comment in the example? However, I think that this example might be a bit special, because we are in a program
scope which doesn't necessarily call the final
procedure on exit but leaves it to the kernel to clean up the used memory after the process terminates. To avoid false positive memory leaks from the C side, manual deconstruction seems like the right thing to do.
By documentation do you mean the Readme? If so, I can I create a PR. Yes, the explicit destruction in the main program scope is a good rule to avoid the false positive when using an application like valgrind.
My question was motivated by my two recent threads:
An artificial example for the auto-finalization in the associate statement, would be a constraint object with a pointer component, that was created using the default structure-constructor directly in the associate statement, instead of declaring at as a (static) target in the main program scope.
I'm interested in the rationale behind preserving explicit destruction of the optimization object instance instead of wrapping this within a finalizer.
I can see a few reasons why'd you keep this as the user's responsibility, primarily to avoid dealing with the odd behavior prescribed by the Fortran standard.