C-Plus-Plus icon indicating copy to clipboard operation
C-Plus-Plus copied to clipboard

fix: remove memory leak in stack

Open vil02 opened this issue 3 years ago • 4 comments

Description of Change

I added the member function deleteAllNodes() into the stack and applied it in the destructor, assignment operator and in the clear method. I have applied address sanitizer and valgrind on test_stack.cpp and test_stack_students.cpp - no further leaks were found. The mentioned files, are the only ones using stack.h.

Checklist

  • [x] Added description of change
  • [x] Relevant documentation/comments is changed or added
  • [x] PR title follows semantic commit guidelines
  • [x] Search previous suggestions before making a new one, as yours may be a duplicate.
  • [x] I acknowledge that all my contributions will be made under the project's license.

Notes: Quickfix of the memory leak in stack.h.

vil02 avatar Oct 12 '22 17:10 vil02

As top(), display() and isEmptyStack() are non modifying methods, you can qualify them with const.

Agree - this implementation needs some cleaning - making it const correct and really testable is something what should be done quickly. But I think this is beyond the scope if this PR.

vil02 avatar Oct 14 '22 08:10 vil02

To remove a few clang-tidy errors, please change the filename to stack.hpp. You might need to update other instances as well.

This change indeed suppressed the errors. But as discussed above: this implementation needs a cleanup.

The entire Awesome CI Workflow run on my side. @Panquesito7 could you please approve the run on your end?

vil02 avatar Oct 14 '22 14:10 vil02

Would you like to work on these files and improve them to our standards? That would fit for another PR, of course.

Panquesito7 avatar Oct 15 '22 00:10 Panquesito7

Would you like to work on these files and improve them to our standards? That would fit for another PR, of course.

I could do that. However there are few things, which I would like to clarify before:

  • tests: should I use some framework for writing them (something like GoogleTest or Boost.Test). I realize that this is a bigger topic.
  • I think for the educational purposes it might be a good idea to stay with raw pointers instead of using smart pointer. Do you agree?
  • there is a similar memory leak in queue.h (cf. #2417).

vil02 avatar Oct 15 '22 08:10 vil02

Would you like to work on these files and improve them to our standards? That would fit for another PR, of course.

I could do that. However there are few things, which I would like to clarify before:

  • tests: should I use some framework for writing them (something like GoogleTest or Boost.Test). I realize that this is a bigger topic.
  • I think for the educational purposes it might be a good idea to stay with raw pointers instead of using smart pointer. Do you agree?
  • there is a similar memory leak in queue.h.
  1. I haven't heard of that tool, but if it makes good self-test implementations @tjgurwara99, thoughts?
  2. Smart pointers are used in modern C++ which we should stick to, IMHO. This will also help others learn smart pointers and make the CI happy as well.
  3. Great finding, thank you! Feel free to fix that if you like in another PR. :)

Panquesito7 avatar Dec 27 '22 23:12 Panquesito7

@Panquesito7 I changed the implementation to use std::shared_ptr and added test_stack.cpp. The old tests are now in test_stack_legacy.cpp.

vil02 avatar Dec 28 '22 20:12 vil02

I think now it everything is ready to be merged.

vil02 avatar Dec 28 '22 20:12 vil02