Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Refactor to use std::unique_ptr instead of naked pointers - #9281

Merged
brianjohnson5972 merged 2 commits into
developfrom
sk-launcher
Jul 21, 2020
Merged

brianjohnson5972 merged 2 commits into
developfrom
sk-launcher

Conversation

@nksanthosh

@nksanthosh nksanthosh commented Jul 6, 2020 •

Copy link
Copy Markdown
Contributor

Change Description

Since delete just marks the memory as "being free for reuse" and lhost does not immediately go out of scope and is used in several places it is safer to use std::unique_ptr instead of naked pointers and the corresponding delete

Change Type

Select ONE

  • Documentation
  • Stability bug fix
  • Other
  • Other - special case

Consensus Changes

  • Consensus Changes

API Changes

  • API Changes

Documentation Additions

  • Documentation Additions

Since delete just marks the memory as "being free for reuse" and lhost does not immediately go out of scope and is used in several places it is safer to set it to nullptr following the delete
@nksanthosh
nksanthosh marked this pull request as ready for review July 6, 2020 02:16
@nksanthosh nksanthosh changed the title Update main.cpp to use nullptr following delete use nullptr following delete Jul 6, 2020
Comment thread programs/eosio-launcher/main.cpp Outdated
if (lhost) {
bindings.emplace_back(move(*lhost));
delete lhost;
lhost = nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to change this to use a std::optional

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to make it a std::unique_ptr.

Comment thread programs/eosio-launcher/main.cpp Outdated
} // for i
bindings.emplace_back( move(*lhost) );
delete lhost;
lhost = nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a local variable, I don't think this is needed, but if we make this change, I think for maintainability we should instead make lhost a unique_ptr and change line 810 and 851 to be "bindings.push_back(*lhost);" so we don't have to keep track of memory. This use case will not loop through this enough times for the moving of memory to make a significant impact.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brian - please take a look at the revised implementation. Thanks.

@nksanthosh nksanthosh changed the title use nullptr following delete Refactor to use std::unique_ptr instead of naked pointers Jul 9, 2020
@nksanthosh
nksanthosh marked this pull request as draft July 13, 2020 20:40
Since delete just marks the memory as "being free for reuse" and lhost does not immediately go out of scope and is used in several places it is safer to use std::unique_ptr instead of naked pointers and the corresponding delete

@johndebord johndebord left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. reset is called under the hood for the assignment operator; so it looks safe to me.
https://stackoverflow.com/questions/16061407/should-i-assign-or-reset-a-unique-ptr

@nksanthosh
nksanthosh marked this pull request as ready for review July 21, 2020 15:21
@brianjohnson5972
brianjohnson5972 merged commit 4297d1b into develop Jul 21, 2020
@jeffreyssmith2nd
jeffreyssmith2nd deleted the sk-launcher branch July 21, 2020 16:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants