Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8457439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:48:29+00:00 2026-06-10T12:48:29+00:00

I have an arduino program where I want to store data in a dynamic

  • 0

I have an arduino program where I want to store data in a dynamic list. For this I use the following struct:

struct Project {
  boolean         status;
  String          name;
  struct Project* nextProject;
};

Now I generate a dynamic amount of Project objects, depending on extern data. I only have the first Project object as variable, the rest i can get through the nextProject pointer.

The generation of the Project objects is done in my loop again every minute. The problem is that from time to time I loos memory till it’s empty.
This is how my main loop looks like:

void loop() {
  webServer(server);
  webClient();

  if (parseTimer(60)) {
    sendRequest();
  }
}

(parseTimer is a non blocking delay function that returns true every 60 seconds; sendRequest generates the Project objects)

My method to measure the memory:

uint8_t* stackptr;
uint8_t* heapptr;

long getFreeMemory() {
  stackptr = (uint8_t *) malloc(4);
  heapptr  = stackptr;
  free(stackptr);
  stackptr = (uint8_t *) (SP);

  return long(stackptr) - long(heapptr);
}

This is the ammount of memory each loop:

1:  4716 *
2:  4716 *
3:  4716 *
4:  4671
5:  4687
6:  4587 *
7:  4736
8:  4587 *
9:  4559
10: 4577
11: 4515
12: 4527
13: 4587 *
14: 4479
15: 4497
16: 4435
17: 4447
18: 4587 *
19: 4399
20: 4417
21: 4355
22: 4367
23: 4587 *
24: 4319

Memory is getting less but after the first few loops, every 5th loop I have 4587 Bytes of free memory. After ~280 loops the program is out of memory, but till there every 5th loop has exactly 4587 Bytes free memory.
Can anyone explain me what could be the cause for this strange behavior and how can I create a better dynamic list that doesn’t leaks memory.

UPDATE

In each loop the Project objects are generated, used and deleted. It does sth. like this:

void sendRequest() {
  // at first it gets some remote data from a server the result is:
  String  names[]  = {"Project 1", "Project 2", "Project 3"};
  boolean states[] = {true, false, true};

  for(int i = 0; i <= projectCount; i++) {
    addProject(names[i], states[i]);
  }
}

// all variables that are not declarated here are declarated in the
// header file of the class
void addProject(String name, boolean state) {
  if (!startProject) {
    startProject = true;

    firstProject.status      = state;
    firstProject.name        = name;
    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;
  }
}

void RssParser::resetParser() { 
  delete ptrToLastProject;
  [...]
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:48:30+00:00Added an answer on June 10, 2026 at 12:48 pm

    After lots of research and writing unit tests for nearly every function of the project I was able to find the mistake.

    In my resetParser() function i did only a delete ptrToLastProject. But there was an other Pointer to this object so the memory was not released. Adding this to the resetParser() function fixed the leak.

    delete firstProject.nextProject;
    

    The reason for this is because the firstProject is on the stack with an pointer to the other projects in the list on the heap. When I delete this pointer all other objects in the list are deleted too by their destructors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fetching data from the following database: I have arduino-box that send that
I currently have the following array in a Java program, byte[] data = new
In an Arduino project, I have build configurations to compile for, and download to,
I have the following code on my Arduino that constantly checks for a serial
I have a robotics type project with an Arduino Uno , and to make
I have to connect the Arduino board I got with Java and get data
I have a constructor for my Arduino-code which is something like the following: class
I have a Python script that writes data packets to an Arduino board through
I have written a program in C which communicates through udp with an Arduino.
I have a project where I need my iPhone to communicate with an Arduino.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.