I developed the Electricity Meter plug-in. The plugin works fine but only 2-3 hours. With each cycle, the amount of free memory is reduced. Help me find a memory leak problem.
Link to plugin:
https://github.com/Andrey2509/ESPEasy/b ... ury230.ino
Please help check code for memory leak
Moderators: rtenklooster, Voyager, BertB, Stuntteam
Forum rules
You have entered the experimental forum, beware!!!
You have entered the experimental forum, beware!!!
- grovkillen
- Core team member
- Posts: 3621
- Joined: 19 Jan 2017, 12:56
- Location: Hudiksvall, Sweden
- Contact:
Re: Please help check code for memory leak
Without looking at the code it's most likely due to assigning variables over and over again each loop run. Static variables don't need to be assigned more than once.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you

ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you



Re: Please help check code for memory leak
Every time you do call "new" and assign it to a pointer, you have to keep track of it and thus call "delete" on the object before assigning a new object to the pointer.
With new you create an object on the heap and you have to make sure it will be deleted.
Why do you create them on the heap?
You may also want to read into smartpointer, shared pointer, etc. about making handling of heap allocated objects a bit easier.
With new you create an object on the heap and you have to make sure it will be deleted.
Why do you create them on the heap?
You may also want to read into smartpointer, shared pointer, etc. about making handling of heap allocated objects a bit easier.
Re: Please help check code for memory leak
I have been looking at the code a bit more and you really should look into memory management of C++.
You:
Any variable which is a little bigger than an int (e.g. an array, or something defined in a struct or class) should be passed on as a reference or const reference when calling a function.
You almost never should have functions that return a pointer, except for special occasions or design patterns.
Start reading about management of objects created on the heap (created with 'new' )
You:
- create a lot of variables on the stack, which you don't use.
- create a lot of variables on the heap, which can be created on the stack just fine.
- don't delete values created on the heap.
- don't pass values by reference to a function, which will allocate a new instance (on the stack) of the variable and then copies all contents to it.
- use static strings in the source code without using the F() macro (is Arduino/AVR specific to store the string in the flash instead of the RAM)
- Let functions return a pointer, but actually return something that isn't a pointer. (for example getCurrent )
- address out-of-bound. For example response[l-2];
Any variable which is a little bigger than an int (e.g. an array, or something defined in a struct or class) should be passed on as a reference or const reference when calling a function.
You almost never should have functions that return a pointer, except for special occasions or design patterns.
Start reading about management of objects created on the heap (created with 'new' )
Who is online
Users browsing this forum: No registered users and 14 guests