【发布时间】:2017-07-16 02:21:24
【问题描述】:
我正在使用带有 MD5 库的 Arduino mega 和带有 SoftwareSerial 的 ESP8266。问题是在 370 次循环后,由于内存不足,Arduino 会自动重启。我使用 FreeMemory 进行故障排除,我注意到问题是每个循环的可用内存减少。这是一种奇怪的行为,因为它仅在我使用 AT 命令和 MD5 时出现,但是如果我将草图分成两个草图,它们可以正常工作而不会出现内存问题。 我的原始草图相当复杂,但为了更清楚,我将示例简化为下面显示的基本代码,行为是相同的,所以如果我修复它,我将能够修复我的原始草图
#include <SoftwareSerial.h>
#include <MemoryFree.h>
#include <MD5.h>
void setup() {
// initialize the digital pin as an output.
Serial.begin(115200);
Serial.println("Starting");
Serial1.begin(115200);
delay(200);
}
// the loop routine runs over and over again forever:
void loop() {
Serial1.println("AT");
delay(100);
Serial.println(Serial1.readString());
Serial.println("-----------");
unsigned char* hash=MD5::make_hash("hello world, this an example");
//generate the digest (hex encoding) of our hash
char *md5str = MD5::make_digest(hash, 16);
//print it on our serial monitor
Serial.println(md5str);
//Give the Memory back to the System if you run the md5 Hash generation in a loop
free(md5str);
Serial.println(freeMemory());
}
谢谢!
【问题讨论】:
-
你确定
hash变量也不需要被释放吗? -
应该这样吗?它不能保留分配的内存以防以后再次需要它吗?
标签: c++ memory arduino md5 esp8266