-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Sketcher: Freeing a non-pointer in App/planegcs/GCS.cpp
#8895
Comments
@abdullahtahiriyo I've assigned to you. If not appropriate, apologies. |
App/planegcs/GCS.cpp
It is ok. Thank you. Thanks André for your observation. If I understand you well, you might be assuming that free is the eponymous C function. However that function takes a pointer as argument. If there weren't another function taking a vector of constraint pointers as argument, the code would not compile at all. The actual function called is this one: FreeCAD/src/Mod/Sketcher/App/planegcs/GCS.cpp Line 4821 in 45001fa
This function iterates the std::vector and deletes an static_cast -ed version of the pointer and clears the vector. As you know, clearing a vector will not release the dynamically allocated memory of the pointers in it. So explicitly deleting the pointers is necessary before clearing the vector. Leaving aside the fact that it is quite old code and things could be done differently (and certainly I will look into it with more calm), what it does ensure is that there is no memory leak when a constraint needs to be removed from the system. I say things could be done differently because, at least now, constraint has a virtual destructor, so if every inherited class provided an appropriate destructor (either the compiler provided default one, which will be virtual due to inheritance, or a specific virtual override where necessary), this type checking and deleting appears to me, at a first glance, it should not be needed at all (dynamic dispatch would do it for you), and then indeed there would no use of reusing the free function for just one constraint and the code could be simplified to a simple delete constraint, as there will be no need to construct the vector just to reuse the free function (which I presume was the intention of the developer here). So, there might be indeed an opportunity to improve the code so that it is more readable (with proper compiler optimizations, the assembly may end up being the same, but code readability is important). But perhaps I should look into this after having some sleep, as now I have been almost 18 hours awake and I may be missing something. Regarding your observation of Please, let me know if you think I am missing something. |
LOL... I am so sorry (again!). :-) |
You should not be sorry. You point out to some code that would benefit from a review. And I am leaving this unread to review it. |
Thanks so much abdullah for your patience and your multi-faceted skills. ❤️ |
Is there an existing issue for this?
Forums discussion
https://forum.freecad.org/viewtopic.php?t=76724
Version
0.21 (Development)
Full version info
I saw the bug in the source code, not in a compiled program.
FreeCAD/src/Mod/Sketcher/App/planegcs/GCS.cpp
Line 641 in 45001fa
Subproject(s) affected?
Sketcher
Issue description
The function constructs a
std::vector<Constraint*> constrvec
. Then, it pushes back a pointer and then callsfree(constrvec)
.This is wrong because
constrvec
is not a pointer. It is a local object that is automatically destroyed.It seems the intention was to free the pushed back pointer. In this case, no
std::vector<Constraint*> constrvec
should be used! There are two possible fixes:removeConstraint()
function.constr
.I do believe we are not supposed to destroy anything. It would be wierd to have
removeConstraint
destroying/freeingconstr
, but not havingaddConstraint
constructing/allocating it. However, it is a possible memory leak that needs to be investigated.Anything else?
I can submit a patch, but I don't know if it is correct to not destruct/free
constr
.Code of Conduct
The text was updated successfully, but these errors were encountered: