0

Specifically the particle Photon, but the following code works locally and on repl.it, but crashes when run on an Arduino:

#include "StringRingBuffer.h"

StringRingBufferNode::StringRingBufferNode(String value) {
  this->value = value;

  this->next = this;
}

StringRingBuffer::StringRingBuffer(int maxSize) {
  this->_maxSize = maxSize;
  this->_currentSize = 0;
  this->count = 0;
}

void StringRingBuffer::addString(String value) {
  if (!this->_firstNode) {
    this->_firstNode = new StringRingBufferNode(value);
    this->_lastNode = this->_firstNode;

    this->_currentSize++;
    this->count++;
  } else if (this->_currentSize < this->_maxSize) {
    StringRingBufferNode *formerFirst = this->_firstNode;
    this->_firstNode = new StringRingBufferNode(value);
    formerFirst->next = this->_firstNode;
    this->_firstNode->next = this->_lastNode;

    this->_currentSize++;
    this->count++;
  } else {
    StringRingBufferNode *formerNext = this->_firstNode->next;

    this->_firstNode->next = new StringRingBufferNode(value);
    this->_firstNode->next->next = formerNext->next;

    this->_lastNode = this->_firstNode->next->next;
    this->_firstNode = this->_firstNode->next;
  }
}

Here is the header file:

#include "application.h"

class StringRingBufferNode {
  public:
    StringRingBufferNode(String);

    String value;
    StringRingBufferNode *next;
  private:
};

class StringRingBuffer {
  public:
    StringRingBuffer(int);

    int count;

    void addString(String);

  private:
    int _maxSize;
    int _currentSize;
    StringRingBufferNode *_firstNode;
    StringRingBufferNode *_lastNode;
};

It seems that commenting out

formerFirst->next = this->_firstNode;
this->_firstNode->next = this->_lastNode;

the program runs without exception (albeit incorrectly.) I'm rather new to C++ so a beginner's explanation of what I'm doing wrong would be fantastic! I wish I had more details about the exception to give, but it's on the microcontroller, so all I know is that it breaks.

EDIT: Link to the working repl.it: https://repl.it/Gh0a/14

EDIT EDIT: I believe I found the solution/problem. I was explicitly testing for this condition: if (!this->_firstNode), and for some reason this does not resolve the same way in Arduino that it does in the repl.it I'd still love for anyone to shed some light onto why this is, otherwise I'll answer the question myself and accept it in two days.

Pete.Mertz
  • 1,302
  • 2
  • 18
  • 34
  • Notes: C++ doesn't throw anywhere near as many exceptions as you may be used to. It assumes you know what you are doing and doesn't generally check that you haven't done something silly unless explicitly asked to. [It's good to know the rules about using underscores in identifiers.](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) Doesn't look like you are breaking them, but forewarned is forearmed. – user4581301 Mar 28 '17 at 23:46
  • The `stringRingBuffer` constructor never initializes `_firstNode` or `_lastNode`. So you cause undefined behaviour when reading `_firstNode` in `addString`. – M.M Mar 29 '17 at 02:40

0 Answers0