ofSerial bug for implicit object verses pointer

Hey has anyone else seen this weird ofSerial bug? If I use an implicit object of type ofSerial and I get a stack error. If I call it as a pointer using new all is well. This pointer way isn’t the same as what I’ve seen in the documentation code I’ve looked at. Any thoughts? Thought I’d bring it up. For example, Joshua Nobel’s code on the forums are all using the implicit method. Did this change?

Another thought - could the implementation of ofToInt could be much less computationally heavy? Why aren’t we just using atoi behind the scene?

Here’s a link to the image with the stack trace:

Lost two very painful debugging and silently crying into my tea days on this one. The fix makes so little sense.

//This code works.
ofSerial * serial;
int sensorPadValue;

void ofApp::setup(){
serial = new ofSerial();
serial->listDevices();
serial->setup(0, 9600);
}
//--------------------------------------------------------------
void ofApp::update(){

//This gets sent to arduino, calling for a new set of readings
serial->writeByte('a');

if(serial->available()){
    
    unsigned char bytesReturned[6]; //000 new line + null terminator
    
    memset(bytesReturned, 0, sizeof(bytesReturned));
    
    // This reads the data now that arduino is sending a response,
    serial->readBytes(bytesReturned, 5);
    //check the data to make sure the new line isn't doing weird stuff
    
    string serialData = (char*) bytesReturned;
    sensorPadValue = ofToInt(serialData);
    cout << sensorPadValue << endl;
    //This process allows the whole process to repeat without
    // getting strange overlapping readings from the encoder:
    serial->flush();
} 
}

This code will give you the bug:

ofSerial serial;
//this is the strange line. If I don’t explicity call the ofSerial construtor all hell breaks loose. The error is Thread 1: EXC_BAD_ACCESS (code=1, address 0xffffff4).

void ofApp::setup(){

  serial.listDevices();
  serial. setup(0, 9600);
}

void ofApp::update(){

  //This gets sent to arduino, calling for a new set of readings
  serial.writeByte('a');
  
  if(serial.available()){
      
      unsigned char bytesReturned[6]; //000 new line + null terminator
      
      memset(bytesReturned, 0, sizeof(bytesReturned));
      
      // This reads the data now that arduino is sending a response,
      serial.readBytes(bytesReturned, 5);
      //check the data to make sure the new line isn't doing weird stuff
      
      string serialData = (char*) bytesReturned;
      sensorPadValue = ofToInt(serialData);
      cout << sensorPadValue << endl;
      serial.flush();
  } 
}


(Ps. Sorry for the edits. I had a hard time getting the code blocks to format from my cut and paste the way I wanted.)

I would guess this could be computer/platform specific? Meaning that by using a pointer you are calling the constructor at runtime where certain resources are available.

I haven’t seen this with ofSerial in particular (I don’t use it often) but I have seen similar behavior with Classes that interface with external hardware

So if you’re having this error a good shot is to explicitly construct. Good to know. The irony? I’ve seen it work implicitly on my own machine with less code in my project. (aka if I just run Josh’s sample it works out just fine from programming interactivity.) That seems super volatile. I’m also using ofxHttpUtils in a different class which makes me think on some root level there’s a memory issue in our framework somewhere deep and painful.

Looking at it closer I am curious what the log looks like if you set ofSetLogLevel(OF_LOG_VERBOSE);

The error looks like it is happening when it is trying to convert a string but that happens multiple times in the code. I am curious in step in 3 buildDeviceList of what string is being passed to the = operator (and leading to the crash)

I’ll circle back around to this as soon as my project is done but the code is up on git now here ->https://github.com/phoenixperry/sparkCore_ofExample
Truth is when I finish this project, I hope to break it into a ton of little examples for oF and share it.

The Serial stuff is just in ofApp right now and to bring the bug back all you’d need to do is implicitly create it. That stuff is all code inside ofSerial and as you can see my call passes no string. That function gets called by default if you implicitly copy if I remember?