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,