-
Notifications
You must be signed in to change notification settings - Fork 1
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
C++ API: made the C++ class Equation to inherit from the C struct EQ #385
Conversation
} | ||
|
||
Equation::~Equation() | ||
{ | ||
if (c_equation) E_free(c_equation); | ||
SW_nfree(this->lec); | ||
SW_nfree(this->clec); |
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.
@jmpplan
Does the function SW_nfree()
reset the passed pointer to NULL
after it has been freed ?
If not, do you know an easy way to check if a pointer is valid ?
if SW_nfree()
does not reset the pointer to NULL
, I can't use:
if(clec != NULL)
{
...
}
to test if the pointer is valid. Do you know what I mean ?
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.
As the argument (here the pointer) is passed by value, it cannot be changed inside SW_nfree()
. So you're right, you cannot use the construct you mentioned. Example.
int MyTestFunction()
{
char* ptr,;
ptr = SW_alloc(10); // ptr contain a pointer
strcpy(ptr, "abc");
SCR_nfree(ptr): // the memory pointed to by ptr is freed, but the variable ptr is unchanged
strcpy(ptr, "abc"); // BUG BUG
return(0);
}
As there is no garbage collector in C. if you forget to free ptr
before the end of the function, the memory is lost...
To solve that (legitimate) issue, the only way I know is to use a leve l of indirection, for example by storing the allocated references (pointers) in a table and then using references to that table to find the real pointer. BTW, that's one of the reason we had implemented the "Swap" group of functions that does exactly that.
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.
Thanks for you answer.
To solve that (legitimate) issue, the only way I know is to use a leve l of indirection, for example by storing the allocated references (pointers) in a table and then using references to that table to find the real pointer. BTW, that's one of the reason we had implemented the "Swap" group of functions that does exactly that.
That's what C++ smart pointers
do.
But in the present case, as I understand your answer, it seems that I have no way to check if a pointer (for example a clec attribute from an instance of EQ) is valid before to call SW_nfree(clec). Am I right ?
To be honest, I am often stuck in the development of my C++ API because I'm trying to free a pointer that has already been freed. Then, to find where that pointer has been freed is very complicated because the C API is very large and some parts are very obscure.
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.
But in the present case, as I understand your answer, it seems that I have no way to check if a pointer (for example a clec attribute from an instance of EQ) is valid before to call SW_nfree(clec). Am I right ?
Yes, you are. So we need a way to avoid freeing NULL pointers (it crashes your program). What I always do after a call to SW_nfree
(or any other *free()
function), is to set the variable to NULL. scr4
checks if a pointer is not NULL before freeing the memory,
To be honest, I am often stuck in the development of my C++ API because I'm trying to free a pointer that has already been freed. Then, to find where that pointer has been freed is very complicated because the C API is very large and some parts are very obscure.
Same answer. The best way if to set the freed pointer to NULL and to call a function that checks that the pointer is not NULL before actually trying to free it. I think that in the IODE API, that's almost always the case.
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.
What I always do after a call to SW_nfree (or any other *free() function), is to set the variable to NULL. scr4 checks if a pointer is not NULL before freeing the memory,
That's the proper way to do. That's an important information because I know now that if the debugger tells me that I'm trying to free an invalid pointer, the problem is somewhere in the C++ code, not in the C API.
I think that in the IODE API, that's almost always the case
almost ? =D
No description provided.