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

Use std::make_unique #2736

Merged
merged 3 commits into from
Mar 18, 2024
Merged

Use std::make_unique #2736

merged 3 commits into from
Mar 18, 2024

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented Mar 17, 2024

This PR improves use of std::unique_ptr by using std::make_unique where appropriate instead of reset(), and also updating some class constructors to use direct assignment.

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r23-use-make_unique-to-make-unique_ptrs

Copy link

what-the-diff bot commented Mar 17, 2024

PR Summary

  • Applied std::make_unique in various parts of the code

    The code now uses std::make_unique in place of the new keyword across a multitude of files and functions. This change enhances memory safety and makes the code easier to maintain. This modification is applied to the constructors of many files like TaskStat.cpp, TcpClientTransport.h, RbootHttpUpdater.h and more.

  • Removed Redundant reset Calls

    This PR includes the removal of redundant reset calls in several files including uart_server.cpp, wifi.cpp, and AccessPointImpl.cpp. This helps streamline the code and remove potential areas of inefficiency.

@slaff slaff added this to the 5.2.0 milestone Mar 18, 2024
@slaff
Copy link
Contributor

slaff commented Mar 18, 2024

@mikee47 can you rebase this PR?

@mikee47
Copy link
Contributor Author

mikee47 commented Mar 18, 2024

Looking at the Codacy issues:

Member variable 'Decompressor::dict' is not initialized in the constructor.

Don't care. block of memory gets overwritten. No advantage really in spending time zeroing it out.

Condition '!decompressor' is always false

That implies std::make_unique can throw exceptions. I've tested it running on Esp8266 and can confirm there is a difference. For example:

	auto mem = new uint8_t[128000];

Gives mem = nullptr, whereas this:

	auto mem = std::make_unique<uint8_t[]>(128000);

Raises a fatal STORE_PROHIBITED exception.

I'm changing this PR to WIP as we need to consider this further.

@mikee47 mikee47 changed the title Use std::make_unique [WIP] Use std::make_unique Mar 18, 2024
@mikee47
Copy link
Contributor Author

mikee47 commented Mar 18, 2024

OK, so the reason why new uint8_t[] and std::make_unique differ is purely because the second one default-initialises the allocated data. If the allocation fails, then attempting to write to location 0 (null) raises the exception. I was aware of this distinction whilst making these changes so removing the WIP tag.

For the Esp8266 at least, new by default does not throw exceptions. Presumably as long as we've disabled exception handling in the C library this is what will happen. Codacy assumes standard behaviour hence the warning.

@mikee47 mikee47 changed the title [WIP] Use std::make_unique Use std::make_unique Mar 18, 2024
@slaff slaff merged commit 07cd096 into SmingHub:develop Mar 18, 2024
45 of 46 checks passed
@mikee47 mikee47 deleted the feature/make_unique branch March 19, 2024 15:16
@slaff slaff mentioned this pull request May 8, 2024
5 tasks
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

Successfully merging this pull request may close these issues.

2 participants