Error for object : incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug

Hi,
I am writing a module (Xcode + OSX 10.9) to communicate with google CSE (custom search engine, it requires an ID and a token). I am using googleCSETask: public Poco::Task (so that in the future I can report progress) and a Poco::TaskManager.

I am sucessfully contacting google and retrieving results, however I receive:

malloc: error for object 0x130350: incorrect checksum for freed object - object was probably modified after being freed. set a breakpoint in malloc_error_break to debug

After reading several posts related to this topic I activated Xcode’s Guard Malloc as follows:

Then Xcode stopped directly at the problem (see the stack trace), however I do not understand what I am doing wrong:

I understand that the auto PTR should be release automatically, but how come it is being invoked twice?
Here is the code (I can attach all the project):

#include "ofApp.h"
#include "Poco/ThreadPool.h"
#include "Poco/TaskManager.h"
#include "Poco/AutoPtr.h"


const std::string THRED_POOL_NAME="SEACRH-TP";

//--------------------------------------------------------------
void ofApp::setup() {
    ofBackground(0);
    ofSetFrameRate(24);
    
    Poco::ThreadPool tp(THRED_POOL_NAME,2, 5, 120);
    Poco::TaskManager tm(tp);
    //Poco::TaskManager tm;

    Poco::AutoPtr<googleCSETask> pST = new googleCSETask ("germany");
    Poco::AutoPtr<googleCSETask> pST1 = new googleCSETask ("great britain");
    
    tm.start(pST);
    tm.start(pST1);
    
    ofLog(OF_LOG_NOTICE,"Number of tasks:" + ofToString(tm.taskList().size()));
    tm.joinAll();
    ofLog(OF_LOG_NOTICE,"call joinAll after");
    ofLog(OF_LOG_NOTICE,"Number of tasks:" + ofToString(tm.taskList().size()));

}

and the code for the Task:

#include "googleCSETask.h"


//Google new search API
const string GOOGLE_SEARCH_BASE_URL="https://www.googleapis.com/customsearch/v1?";

//api key
const string API_KEY = "";

//custom search engine ID
const string SEARCH_ENGINE_ID = "";

const string HTTPS_HEADER="";

const Poco::Timespan MAX_TIMEOUT(30, 0);

const std::string DEFAULT_CA_LOCATION = "ssl/cacert.pem";

const std::string BASE_QUERY_URL=GOOGLE_SEARCH_BASE_URL + "key=" + API_KEY + "&cx=" + SEARCH_ENGINE_ID + "&q=";


//--------------------------------------------------------------
void googleCSETask::runTask(){
    ofLog(OF_LOG_NOTICE,"Thread ID:" + ofToString(Poco::Thread::current()->id()) +"\n");
    googleCSETask::result=searchTerm(googleCSETask::term);
    //ofLog(OF_LOG_NOTICE,"RESULT:" + result);
}


//--------------------------------------------------------------
googleCSETask::googleCSETask(std::string term) : Task("googleCSETask") {
    googleCSETask::term=term;
    ofLog(OF_LOG_NOTICE,"baseURL:" + BASE_QUERY_URL);
}

//--------------------------------------------------------------
std::string googleCSETask::searchTerm(std::string term) {
    ofLog(OF_LOG_NOTICE,"Looking for term:" + term +"\n");
    string encodedTerm = urlEncode(term);
    string googleImgURL = BASE_QUERY_URL + encodedTerm + "&alt=json";
    ofLog(OF_LOG_NOTICE,"queryURL:" + googleImgURL);
    return get(googleImgURL);
}


//--------------------------------------------------------------
std::string googleCSETask::get(string url) {
    ofLog(OF_LOG_NOTICE,"Thread ID:" + ofToString(Poco::Thread::current()->id()) +"\n");
    ofLog(OF_LOG_NOTICE,"Retriev URL:" + url +"\n");
    std::string responseText;
    
    //Poco::RWLock lock;
    //Poco::Random rnd;
    
    try {
        Poco::URI uri(url);
        ofLog(OF_LOG_NOTICE, "http/https?: " + uri.getScheme());
        std::string path(uri.getPathAndQuery());
       ...
      /// get the response
        if(status != Poco::Net::HTTPResponse::HTTP_OK) { // 200 is an OK HTTP response
            std::cout << "Got bad response: " << status << endl;
            return ofToString(status);
        }
        else{
            Poco::StreamCopier copier;
            copier.copyToString(rs, responseText);
            return responseText;
        }
    }
    catch (Poco::Exception& error) {
        // print any errors
        ofLog(OF_LOG_ERROR, "URL error: " + error.displayText());
        //throw Poco::ApplicationException("HTTPRequest Error:", error);
        return error.displayText();
    }
}

BTW, what is the best way to wait for a Poco::task to terminate? Is TaskManager->joinAll() the way to go? Or rather using a NotificationHandler?

Documentation for Poco::task is here: https://github.com/pocoproject/sandbox/blob/master/Array/branches/Foundation/Foundation/testsuite/src/TaskTest.cpp

Thanks,

Hey there, It’s difficult to answer your question directly – but you might look at

and

https://github.com/bakercp/ofxHTTP/tree/master/example_basic_client_loader

In the examples, I use Poco::Task and Poco::TaskManager extensively for complex HTTP/S requests, including google searches etc.

There is a bug in the older version of Poco::Notification center (referenced here in my code https://github.com/bakercp/ofxTaskQueue/blob/master/libs/ofxTaskQueue/include/ofx/TaskQueue.h#L585-L593) that is fixed in later versions of Poco, but it may be relevant to your situation.

Also, there is a pretty thorough review of ofxTaskQueue here:

Also, with ofxHTTP you can set custom cookies, tokens, etc for the search. Additionally ofxTaskQueue and the ofxHTTP client queue already report download progress, etc.

Check out

https://github.com/bakercp/ofxHTTP/blob/master/libs/ofxHTTP/include/ofx/HTTP/DefaultClientTask.h
https://github.com/bakercp/ofxHTTP/blob/master/libs/ofxHTTP/include/ofx/HTTP/DefaultClientTaskQueue.h

And their correspending implementations …

Also, all is tested on OSX, window, linux, pi, etc.

Dear Christopher,
Thank you very much for the replies, the DefaultClientTask.h looks very nicely written.
I did actually read some of your examples, and thought they would be too complicated, but I may change my mind now that I directly used Poco:Task.

Would I be able to use ofxHTTP with HTTPS, without a certificate like so:(?)

const string HTTPS_HEADER="ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH";

Poco::Net::Context::Ptr context = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "",
                                                                 Poco::Net::Context::VERIFY_NONE,
                                                                 9, false,
                                                                 HTTPS_HEADER);
        Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(), context );

Thanks,

Using:
tm.start(pST.duplicate()) resolves the issue.
Do you have an idea why?