Fix memory leak in atom.c#362
Conversation
Fix memory leak in _lldpctl_do_something when error occurs after state_data has been duplicated.
|
I am unsure if this is the right fix. With your modification, the data is freed in all cases. So, on next call, it's not here anymore. We store it in the connection to ensure we don't request for several stuff in parallel (the In which case did you hit a memory leak? It may happen if there is a serialization error or if you run into an invalid state. But this also means you have some programming error elsewhere. |
|
I ran into the memory leak when invoking I understand that the |
|
Let me rephrase the latter part: with your explanation I would expect that the |
|
The |
|
Understood. It sounds a bit like needing something in conn to know that a free is needed for state_data. But I for sure see how my solution will not be nice for async mode. |
|
Another question: how do you recover from the error? The state machine is in a bad state, I think you need to tear down the connection? |
The state data is used to ensure we don't interleave requests of the same kind (eg requesting data for eth0, then for eth1 while eth0 is running). The data was freed only when reaching `CONN_STATE_IDLE` again. Otherwise, there was a memory leak. This is an attempt to rework the code to avoid the memory leak. However, we still get a memory leak on error case because we don't know the difference between an hard error case or simply a request to wait for more bytes. See #362 for the discussion.
|
I have pushed an attempt to implement your suggestion (doing the I think a simpler solution would be to remove this safe guard since you uncover a condition that happens in valid code while the safe guard was here to catch invalid code. |
|
First of all: I agree, this fix will not help in my error situation, the leak will still be there. I see your point in not being able to see the difference between a hard error case or waiting for more bytes. In our error case the leak will still be there. |
|
Yes, we could free it when tearing down the connection. I had also other ideas:
|
|
All would work for me. I get the idea for the safeguard, at least the way I am using my client it will not be needed. But in general removing the safeguard could be a bit too much, but you are a better judge of that. Using a fixed-size allocation would be fine, I actually like that one. It removes the whole hassle of having to keep track of the allocation. This is my preferred solution, it would mean atom.c can largely revert to what it was. What is your preference? |
The state data is used to ensure we don't interleave requests of the same kind (eg requesting data for eth0, then for eth1 while eth0 is running). The data was freed only when reaching `CONN_STATE_IDLE` again. Otherwise, there was a memory leak. To avoid the memory leak, we avoid use a static allocation instead. See #362 for the discussion.
|
I am pushing a branch with static allocation. Let's see if it works with the tests. However, fixing the case where a client has a reference to a port and tries to use it while the port doesn't exist anymore would still be interesting. It doesn't seem straightforward. We could return an empty port, but this may break existing clients not checking this case. |
|
Thank you for the discussion so far and for the new commit, I will use that - reverting my PR. For my situation this is now covered, but I understand your broader point. Not sure what to do with this PR now though - should I close it? |
The state data is used to ensure we don't interleave requests of the same kind (eg requesting data for eth0, then for eth1 while eth0 is running). The data was freed only when reaching `CONN_STATE_IDLE` again. Otherwise, there was a memory leak. To avoid the memory leak, we avoid use a static allocation instead. See #362 for the discussion.
|
It will automatically close once the commit will be merged. |
|
Thank you, good to know. Will wait for the final commit, I saw that the tests failed. |
Fix memory leak in _lldpctl_do_something when error occurs after state_data has been duplicated. The function has multiple return statements after the strdup, the conn->state_data was only freed in the happy flow. Fixed by tracking the duplication and jumping to an end label when needing to return, freeing the duplication whenever needed.