Please help check code for memory leak

Moderators: rtenklooster, Voyager, BertB, Stuntteam

Forum rules
You have entered the experimental forum, beware!!!
Post Reply
Message
Author
Andrey2509
New user
Posts: 4
Joined: 25 May 2018, 17:30

Please help check code for memory leak

#1 Post by Andrey2509 » 30 Sep 2018, 19:01

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

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Please help check code for memory leak

#2 Post by grovkillen » 30 Sep 2018, 19:44

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 :idea: :idea: :idea:

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Please help check code for memory leak

#3 Post by TD-er » 30 Sep 2018, 23:11

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.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Please help check code for memory leak

#4 Post by TD-er » 30 Sep 2018, 23:29

I have been looking at the code a bit more and you really should look into memory management of C++.

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];
So my advice, please start reading into the differences of heap and stack allocation (use stack allocation, unless you really need to use the heap)
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' )

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests