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
Updated basic_free_stack to check if basic variable was stack allocated before freeing #2011
base: master
Are you sure you want to change the base?
Conversation
Once I get a green flag, I'll add a test too |
Not sure why one of the tests fail |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can basic_new_stack
ever return NULL
? or rather put NULL
in s->m
? If that is the case shouldn't we always have the check in basic_free_stack
?
Yes that makes sense. So I have been using |
There are pros and cons I guess. One could see it as the responsibility of the caller to make sure that the free function is only called on initialized objects, but it is also not possible to check for this using the C-API. I checked how gmp does for its |
Yeah now that I think through it, I guess we should do the check inside |
Yes, I agree. I think you should do this change |
I've made the change, also went through |
Co-authored-by: Ondřej Čertík <ondrej@certik.us>
At the very least we should put an assert statement that the pointer is not null. Whether we should always do the check --- I don't think we should. The stack variable is typically a local variable, which will not be null, even if it is not allocated. So for application programs that are hand written, this check will not actually trigger anyway, if the variable is not allocated, it will be a dangling pointer and it will segfault. I would create a separate function for this, as you did initially. You can put an assert statement into We can later decide to just always do it, but for now I would keep it separate. One of the main goals of SymEngine is performance, so we should only do operations that are needed, and allowing a path for not doing an operation if the user knows it is not needed. |
Sure, I think that sounds reasonable. Let me think through this again ! |
I agree with @certik about the NULL being uncertain. What I missed in the comparison with libgmp was the call to |
The definition of
In C I don't think arrays can be |
So basically everything arises from the issue on Lpython (lcompilers/lpython#2614) . We are trying to replicate the basic
And then we can use the
|
So something that doesn't work (results into Segmentation Fault) is the following
|
Ok, thanks. The use of void pointers and casting here makes me a bit wary. It seems to me that this situation couldn't arise when using the symengine C-API "correctly". So the if (stack0 != NULL) {
basic_free_stack(stack0);
} at the end? My feeling is that this function would add confusion to the symengine API if added. |
We need to add tests. We are still not sure if this is the way to go, but if we need it, this PR is how to do it. So let's keep it in Draft mode for now, and we'll finish our work in LPython to see what exactly we need. Thanks @rikardn for your feedback! |
This is a function which frees the basic variable only if it has been allocated. The use case comes from here