I have problems with my code, building a website body.
The variable "String htmlContent" contains the whole html body including some placeholders like %%JAVASCRIPT%% and %%IPADDRESS%% etc.
"String htmlJS" contains some javascript code
With every web request from a client I build the website content and replace a lot of placeholders with content of other variables. Sometimes this fails JUST for the htmlJS String.
Do not use the "String" class on arduino's, it may be causing memory corruption and it is impossible to predict how it allocates memory which is sparse.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
"If" you do want to use 'S'trings to create websites, keep them local variable, start over every time you create a page String thePage=""; and add any pieces that you want to add sequentially. then send that as a whole and as the function is terminated the memory will be freed. It is apparently slow, but on a board with an ESP-core it is safe (enough)
Deva_Rishi:
"If" you do want to use 'S'trings to create websites, keep them local variable, start over every time you create a page String thePage=""; and add any pieces that you want to add sequentially. then send that as a whole and as the function is terminated the memory will be freed. It is apparently slow, but on a board with an ESP-core it is safe (enough)
that's actually what I do. It's not a global variable
StefanHaupt:
that's actually what I do. It's not a global variable
Then why do you use 'replace' ? just start over with a new String every time, have chunks of 'const char *' that you can use to add and add all that is beyond that with '+='
I've now "fixed" the problem by just embedding the javascript String content directly into the htmlContent String content. Originally I thought it makes sense to split both code parts into two variables. Now the merge was the easiest fix, so I went for this.