EXC_BAD_ACCESS with curl buffer

Hello!

I tried to use libcurl in my application.

I took HelloWorld application and replaced handle of the button:

void HelloWorld::menuCloseCallback(CCObject* pSender) {

    std::string url("http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json");

    CURL *curl;
    CURLcode res;
    std::string buffer;

    curl_global_init( CURL_GLOBAL_ALL );

    curl = curl_easy_init();

    if( curl )
    {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); // tell us what is happening
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HelloWorld::writer);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        if (res == CURLE_OK && !buffer.empty()) {
            std::cout<

and added writer:

size_t HelloWorld::writer( char *data, size_t size, size_t nmemb, std::string *writerData ) {
    size_t retValue = 0;

    if(writerData != NULL)
    {
        writerData->append(data, size * nmemb);
        retValue = size * nmemb;
    }

    return retValue;
}

Thanks to guys from: http://www.cocos2d-x.org/boards/6/topics/6944.

But code CCLog(“buffer: %s”, buffer.c_str()) raise error: EXC_BAD_ACCESS.

Unfortunately in curl test I did not find code working with buffer, only with result code.

What do I do wrong? What kind of magic should I use to make it working?

Thank you.

is that writer function should be static?

It’s static. Should not it?

yes, it should be.

your situation seems I already experienced but I do not remember it now sorry…

I’ve found the error. It’s too easy but I lost a day :slight_smile: :

CCLog("buffer: %s", buffer.c_str());                      // DOES NOT WORK
CCLog("buffer: %s", buffer.substr(0, 10).c_str());        // WORKS

I hope it will be useful for somebody.