-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert most examples to using "new" for variable pointer creation
- Loading branch information
Showing
10 changed files
with
659 additions
and
685 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
b1a619e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nearly ever forum and tutorial I've read warns not to use "new" to create objects in Arduino code. Especially if used creating objects in the loop, it could lead to lots of space being taken by objects that are never destroyed which would eventually cause the whole thing to crash because it's out of memory. The modular sensors code has been using "new" for ages to create the sensors and variables at the global scope (not in the loop) and it's never been a problem.
Forums also caution against any constructors that require arguments or call any arguments within the code of the constructor. This is because the order in which objects are created in memory (and assigned an address) cannot be controlled and "may possibly change in future versions." So, potentially, a constructor calling external values (as input arguments) or functions may end up trying to call something that doesn't yet exist. This is why most Arduino classes have useless constructors and require a "begin" function of some sort to do anything. Again, I'd written modular sensors to do all kinds of object linking in the constructor and it's been working fine.
Anyway... so someone pointed out on one of the forums that there was an issue with calculated variables not working. I started looking into it and turned on all kinds of debugging and saw all sorts of objects being recreated where they shouldn't have been right before things crashed and got paranoid that it was one of the two problems above. So I started rewriting everything to have empty constructors and begin functions. Turns out, nope, that was never the problem. It was an attempt to call to a null pointer of the non-existent parent sensor of the calculated variable that I'd missed in one of the timing loops. Ugh. (53a96a3)
Anyway, since I'd done so much work in writing everything to be more "compiler safe" I thought I'd make it backwards compatible (so able to call either a useless (safe) or useful (possibly unsafe) constructor) and I'd do some testing. Well.. after a lot of ironing out bugs, it turns out that.. I can get it to compile either way, it works either way, and contrary to much advice, the compiled program is actually slightly smaller and there is slightly more free RAM available when objects are created via calls to the "new" operator. Go figure. So I'm setting up most of the examples to do things the "unsafe" way and leaving code in place so if things change for some future compiler version or processor, it's ready to go.
b1a619e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey thanks for the explanation. Embedded C++ has been on quite a journey to describe what is "safe" -and extremely valuable part of arduino discussion is the detail how it operates and problems to be aware with constructor chains.
The really value in any embedded software - especially as it gets larger is ...... testing ..... testing .... and that with good stable tools, and maintains system stability.
IMHO there is value in the process you've gone through, as it can be used to managed the initialization of the machine better.... but that's a very deep subject ...
again many thanks for sharing.