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

Various code optimizations #647

Merged
merged 3 commits into from
Jan 3, 2020

Conversation

mkfrey
Copy link
Contributor

@mkfrey mkfrey commented Dec 19, 2019

  • In the migration to ArduinoJson v6 I've introduced a bug disallowing the usage of null as wifi.password. Commit 25f6b8c fixes this.
  • The parameter ota.enabled was treated as optional in the config load code, but not in the validation code. Commit 64ca0ba will make ota.enabled optional and default to disabled OTA.
  • In Config::getSafeConfigFile(), a char[] buffer gets allocated within a unique_ptr, only to be copied and and then discarded at the end of the method. Commit 60ca263 removes this unnecessary step.

…etworks

Additionally, this commit will now explicitly allow to omit `wifi.password`
from the configuration. Leaving it out will be treated as setting it to `null`.
* OTA will be disabled by default if `ota.enabled` is not provided
@mkfrey
Copy link
Contributor Author

mkfrey commented Dec 21, 2019

Force pushed the last commit to replace free(safeConfigFile) with delete safeConfigFile, since the allocation was done using new.

Copy link
Collaborator

@bodiroga bodiroga left a comment

Choose a reason for hiding this comment

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

Hi @codefrey!

Thank you very much for your work, we really appreciate it! All your proposed changes seem fine, although I don't know what to say about the "delete safeConfigFile" call. I would like to know what @kleini says about it 😉

Also, I don't know if @davidgraeff wants to review the changes 👍

@@ -472,7 +472,7 @@ void BootNormal::_advertise() {
{
char* safeConfigFile = Interface::get().getConfig().getSafeConfigFile();
packetId = Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$implementation/config")), 1, true, safeConfigFile);
free(safeConfigFile);
delete safeConfigFile;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can anyone else comment on this? My C++ skills are not good enough to review this specific change, sorry.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, understood. The safeConfigFile is created in

char* jsonString = new char[jsonBufferLength];

and 'delete' is the correct method to release memory when objects are created by the "new" expression (https://www.geeksforgeeks.org/delete-in-c/).

LGTM!

@stritti
Copy link
Collaborator

stritti commented Jan 1, 2020

LGTM

return strdup(jsonString.get());
char* jsonString = new char[jsonBufferLength];
serializeJson(jsonDoc, jsonString, jsonBufferLength);
return jsonString;
Copy link
Member

Choose a reason for hiding this comment

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

It's better than before. But you should never return an allocated piece of memory as raw pointer in modern C++.

Copy link
Member

@davidgraeff davidgraeff left a comment

Choose a reason for hiding this comment

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

LGTM and fixes a bug, but there are still places for improvements.

@davidgraeff davidgraeff merged commit 050e68f into homieiot:develop-v3 Jan 3, 2020
@mkfrey
Copy link
Contributor Author

mkfrey commented Jan 4, 2020

@davidgraeff I thought about using the String class, but String does not implement move semantics so I kept the pointer. It might make sense to replace it with an unique_ptr though.

@mkfrey mkfrey deleted the develop-v3-refactor branch March 13, 2020 10:31
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.

4 participants