Loading ofHttpResponse data crashes app

I’m building an app that downloads a small XML file from a server every 10 seconds using ofLoadURLAsync(). The file downloads but the app crashes when the buffer data is accessed. Sometimes it will crash on the 2nd or 3rd load.

I saw this has been raised before here but this didn’t solve the problem:
https://github.com/openframeworks/openFrameworks/issues/1994

Here’s the code. I’ve basically copied the imageLoaderWebExample and replaced the image stuff for XML:

testApp.h

  
  
#pragma once  
#include "ofMain.h"  
class testApp : public ofBaseApp{  
    public:  
	void setup();  
	void draw();  
	void exit();  
	void keyPressed(int key);  
	void urlResponse(ofHttpResponse & response);  
        bool loading;  
};  
  

testApp.cpp

  
  
#include "testApp.h"  
//--------------------------------------------------------------  
void testApp::setup(){  
    loading = false;  
    ofRegisterURLNotification(this);  
}  
  
//--------------------------------------------------------------  
void testApp::urlResponse(ofHttpResponse & response){  
    if(response.status == 200 && response.request.name == "tag_index"){  
        cout << "Loaded XML" << endl;  
        ofXml totalXML;  
        totalXML.loadFromBuffer(response.data);  
        int latestTagTotal = ofToInt(totalXML.getValue(""));  
        cout << "- data loaded from xml - " << latestTagTotal <<endl;  
        loading = false;  
    }else {  
	cout << response.status << " " << response.error << endl;  
	if(response.status!=-1) loading=false;  
    }  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){      
    ofBackground((sin(ofGetElapsedTimef()* 10) + 1) * 50);  
}  
  
//--------------------------------------------------------------  
void testApp::exit() {  
    ofUnregisterURLNotification(this);  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
    if (!loading)  
        ofLoadURLAsync("[https://dl.dropboxusercontent.com/u/1619383/cell/index.xml","tag-index"](https://dl.dropboxusercontent.com/u/1619383/cell/index.xml","tag-index"));  
	loading = true;  
}  
  

if I uncomment the following:

  
  
totalXML.loadFromBuffer(response.data);  
int latestTagTotal = ofToInt(totalXML.getValue(""));  
cout << "- data loaded from xml - " << latestTagTotal <<endl;  
  

The app runs without a problem.

Am I missing something? This is crashing on both osx and Windows 7.

OK I managed to fix this. It seems that the following line:

tagIndexXml.loadFromBuffer(tagIndexData);

Should not be called in urlResponse(). No idea why. I’m sure there’s a brilliant reason. No time to investigate. Just moved this bit to the update function.

Here’s the code if anyone has a similar problem:

testApp.h

  
  
  
#pragma once  
#include "ofMain.h"  
  
class testApp : public ofBaseApp{  
public:  
    void setup();  
    void update();  
    void draw();  
    void exit();  
    void keyPressed(int key);  
    void urlResponse(ofHttpResponse & response);  
      
    ofXml tagIndexXml;  
    string tagIndexData;  
    int latestTagTotal;  
    bool isLoading;  
    bool isNewIndexXml;  
};  
  

testApp.cpp

  
  
#include "testApp.h"  
  
void testApp::setup(){  
	isLoading = false;  
    isNewIndexXml = false;  
	ofRegisterURLNotification(this);  
}  
  
void testApp::urlResponse(ofHttpResponse & response){  
	if(response.status == 200 && response.request.name == "tag_index_load"){  
        tagIndexData = response.data.getText();  
        isNewIndexXml = true;  
        isLoading = false;  
	}else{  
		cout << response.status << " " << response.error << endl;  
		if(response.status!=-1) isLoading = false;  
	}  
}  
  
void testApp::update(){  
    if (isNewIndexXml){  
        isNewIndexXml = false;  
        tagIndexXml.loadFromBuffer(tagIndexData);  
        latestTagTotal = ofToInt(tagIndexXml.getValue(""));  
        cout << "loaded xml - latestTagTotal = " << latestTagTotal << endl;  
    }  
}  
  
void testApp::draw(){  
    ofBackground((sin(ofGetElapsedTimef()* 10) + 1) * 50);  
    ofSetColor(255);  
    ofDrawBitmapString("tag index: " + ofToString(latestTagTotal), ofPoint(10, 10));  
}  
  
void testApp::exit(){  
	ofUnregisterURLNotification(this);  
}  
  
void testApp::keyPressed(int key){  
    if (!isLoading)  
        ofLoadURLAsync("[https://dl.dropboxusercontent.com/u/1619383/cell/index.xml","tag-index-load"](https://dl.dropboxusercontent.com/u/1619383/cell/index.xml","tag-index-load"));  
	isLoading = true;  
}  
  

1 Like