Can this code cause a memory leak (Arduino)
Posted
by
tbraun89
on Stack Overflow
See other posts from Stack Overflow
or by tbraun89
Published on 2012-11-20T10:57:49Z
Indexed on
2012/11/20
10:59 UTC
Read the original article
Hit count: 221
I have a arduino project and I created this struct:
struct Project {
boolean status;
String name;
struct Project* nextProject;
};
In my application I parse some data and create Project
objects. To have them in a list there is a pointer to the nextProject in each Project
object expect the last. This is the code where I add new projects:
void RssParser::addProject(boolean tempProjectStatus, String tempData) {
if (!startProject) {
startProject = true;
firstProject.status = tempProjectStatus;
firstProject.name = tempData;
firstProject.nextProject = NULL;
ptrToLastProject = &firstProject;
} else {
ptrToLastProject->nextProject = new Project();
ptrToLastProject->nextProject->status = tempProjectStatus;
ptrToLastProject->nextProject->name = tempData;
ptrToLastProject->nextProject->nextProject = NULL;
ptrToLastProject = ptrToLastProject->nextProject;
}
}
firstProject
is an private instance variable and defined in the header file like this:
Project firstProject;
So if there actually no project was added, I use firstProject
, to add a new one, if firstProject
is set I use the nextProject
pointer.
Also I have a reset()
method that deletes the pointer to the projects:
void RssParser::reset() {
delete ptrToLastProject;
delete firstProject.nextProject;
startProject = false;
}
After each parsing run I call reset()
the problem is that the memory used is not released. If I comment out the addProject
method there are no issues with my memory. Someone can tell me what could cause the memory leak?
© Stack Overflow or respective owner