#include “curl/curl.h”
struct MemoryStruct
{
char memory;
size_t size;
};
static size_t
WriteMemoryCallback
{
size_t realsize = size nmemb;
struct MemoryStruct mem = userp;
mem->memory = realloc;
if
{
/ out of memory! */
printf(“not enough memory (realloc returned NULL)”);
exit(EXIT_FAILURE);
}
memcpy(&(mem~~>memory[mem~~>size]), contents, realsize);
mem~~>size += realsize;
mem~~>memory[mem~~>size] = 0;
return realsize;
}
CCSprite* getFriendPicture
{
CURL curl_handle;
struct MemoryStruct chunk;
chunk.memory = malloc; / will be grown as needed by the realloc above /
chunk.size = 0; / no data at this point /
curl_global_init;
/ init the curl session /
curl_handle = curl_easy_init;
/ specify URL to get /
curl_easy_setopt;
/ send all data to this function /
curl_easy_setopt;
/ we pass our ‘chunk’ struct to the callback function /
curl_easy_setopt&chunk);
/ some servers don’t like requests that are made without a user-agent
field, so we provide one /
curl_easy_setopt;
/ get it! /
curl_easy_perform;
/ cleanup curl stuff /
curl_easy_cleanup;
CCImage img = new CCImage;
img~~>initWithImageData((void**)chunk.memory, chunk.size, CCImage::kFmtPng);
cocos2d::CCTexture2D** texture = new cocos2d::CCTexture2D();
texture->initWithImage(img);
CCSprite* sprite = CCSprite::spriteWithTexture(texture);
if(chunk.memory)
{
free(chunk.memory);
}
/* we’re done with libcurl, so clean it up */
curl_global_cleanup();
return sprite;
}
“mem~~>memory " is changed NULL, so call “exit;”
why "mem~~>memory” is changed Null?