Skip to content
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

Vector elements should be freed using myvector.erase() #643

Open
jesshowe opened this issue Mar 12, 2024 · 2 comments
Open

Vector elements should be freed using myvector.erase() #643

jesshowe opened this issue Mar 12, 2024 · 2 comments

Comments

@jesshowe
Copy link

delete this->eclipseOutMsgs.at(c);

Hi, according to documentation here, vector's erase() function should be used to remove an item from the vector and subsequently destroy it. We've run into problems where the vector still tracked a previously freed item and then tried to free it again, causing a segfault.

Also, this applies to all modules (I just included the above as an example case), and we implemented our fix like this:
this->eclipseOutMsgs.erase(this->eclipseOutMsgs.begin()+c);

@sassy-asjp
Copy link
Contributor

sassy-asjp commented Mar 13, 2024

erase destroys the element so shouldn't be automatically calling delete on raw pointers. That would only work if the elements were unique_ptr or something, otherwise the erase operation should leak memory.

@jesshowe
Copy link
Author

Hi sassy-asjp,
Thanks for the reply. I've researched this more and it looks like if you have to use raw pointers you should delete them, and THEN call erase so the vector doesn't count them as part of its list anymore. Is there any reason, though, why you're not using smart pointers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants