<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[curl und segfault]]></title><description><![CDATA[<p>Tagchen,<br />
ich hab folgendes:<br />
ein Programm, das eine Internetseite in ein struct schreiben soll.<br />
der eigentliche Code, der die URL entgegen nimmt und die Seite &quot;ansurft&quot; sowie die daten in das struct schreibt ist fast 1:1 eines der Beispiele zur libcurl, nur eben in eine Klasse gequetsch.<br />
die klasse:</p>
<pre><code class="language-cpp">#include &quot;config.h&quot;
#include &quot;http.h&quot;
#include &quot;Version.h&quot;
#include &lt;curl/curl.h&gt;
#include &lt;curl/curlver.h&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstring&gt;

using namespace std;

void *HTTP::myrealloc(void *ptr, size_t size) {
        /* There might be a realloc() out there that doesn't like reallocing
         NULL pointers, so we take care of it here */
        if (ptr)
                return realloc(ptr, size);
        else
                return malloc(size);
}

size_t HTTP::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
        size_t realsize = size * nmemb;
        struct MemoryStruct *mem =( struct MemoryStruct *)data;

        mem-&gt;memory = (char *)myrealloc(mem-&gt;memory, mem-&gt;size + realsize + 1);
        if (mem-&gt;memory) {
                memcpy(&amp;(mem-&gt;memory[mem-&gt;size]), ptr, realsize);
                mem-&gt;size += realsize;
                mem-&gt;memory[mem-&gt;size] = 0;
        }
        return realsize;
}

HTTP::HTTP(const char * p_prj_url, const char * p_file, char * p_uid) {
        request_uri.append(p_prj_url);
        request_uri.append(p_file);
        request_uri.append(p_uid);

        cout &lt;&lt; &quot;prj_url: &quot; &lt;&lt; p_prj_url &lt;&lt; endl
             &lt;&lt; &quot;p_file: &quot; &lt;&lt; p_file &lt;&lt; endl
             &lt;&lt; &quot;p_uid: &quot; &lt;&lt; p_uid &lt;&lt; endl
             &lt;&lt; &quot;request_uri: &quot; &lt;&lt; request_uri &lt;&lt; endl;

        chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
        chunk.size = 0; /* no data at this point */
        cout &lt;&lt; &quot;no data at this point&quot; &lt;&lt; endl;
}

HTTP::~HTTP() {
        /* cleanup curl stuff */
        curl_easy_cleanup(curl_handle);

        if (chunk.memory) {
                free(chunk.memory);
        }
}

char * HTTP::get_code(void) {
        return chunk.memory;
}

size_t * HTTP::get_size(void) {
        return &amp;chunk.size;
}

void HTTP::performRequest(void) {
        curl_global_init(CURL_GLOBAL_ALL);

        /* init the curl session */
        curl_handle = curl_easy_init();

        /* specify URL to get */
        curl_easy_setopt(curl_handle, CURLOPT_URL, request_uri.c_str());

        /* send all data to this function  */
        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &amp;HTTP::WriteMemoryCallback);

        /* we pass our 'chunk' struct to the callback function */
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&amp;chunk);

        /* some servers don't like requests that are made without a user-agent field, so we provide one */
        cout &lt;&lt; &quot;setting user-agent: &quot; &lt;&lt; &quot;boinc_data/&quot; &lt;&lt; BOINCDATA_VERSION &lt;&lt; &quot; using libcurl/&quot; &lt;&lt; LIBCURL_VERSION &lt;&lt; endl;
        curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, &quot;boinc_data/&quot;BOINCDATA_VERSION&quot; using libcurl/&quot;LIBCURL_VERSION);

        const char * encoding = &quot;UTF-8&quot;;
        cout &lt;&lt; &quot;setting encoding: &quot; &lt;&lt; encoding &lt;&lt; endl;
        curl_easy_setopt(curl_handle, CURLOPT_ENCODING, encoding);

        curl_easy_perform(curl_handle);
        cout &lt;&lt; &quot;Data dump: &quot; &lt;&lt; chunk.memory &lt;&lt; endl
             &lt;&lt; &quot;Size: &quot; &lt;&lt; chunk.size &lt;&lt; endl;
}
</code></pre>
<p>alles was cout betrifft ist normalerweise nicht da, nur zum einfacheren debugging. Wenn ich das Programm mit den entsprechenden Parametern aufrufe, kommt das raus:</p>
<blockquote>
<p>prj_url: <a href="http://setiweb.ssl.berkeley.edu/" rel="nofollow">http://setiweb.ssl.berkeley.edu/</a><br />
p_file: show_user.php?userid=<br />
p_uid: 1234<br />
request_uri: <a href="http://setiweb.ssl.berkeley.edu/show_user.php?userid=1234" rel="nofollow">http://setiweb.ssl.berkeley.edu/show_user.php?userid=1234</a><br />
no data at this point<br />
setting user-agent: boinc_data/0.0.1 using libcurl/7.16.2<br />
setting encoding: UTF-8<br />
Speicherzugriffsfehler</p>
</blockquote>
<p>mit gdb debugged:</p>
<blockquote>
<p>prj: 28<br />
uid: 1<br />
mode: 0<br />
file: show_user.php?userid=<br />
instantiating the HTTP-Class...<br />
prj_url: <a href="http://setiweb.ssl.berkeley.edu/" rel="nofollow">http://setiweb.ssl.berkeley.edu/</a><br />
p_file: show_user.php?userid=<br />
p_uid: 1<br />
request_uri: <a href="http://setiweb.ssl.berkeley.edu/show_user.php?userid=1" rel="nofollow">http://setiweb.ssl.berkeley.edu/show_user.php?userid=1</a><br />
no data at this point<br />
setting user-agent: boinc_data/0.0.1 using libcurl/7.16.2<br />
setting encoding: UTF-8</p>
<p>Program received signal SIGSEGV, Segmentation fault.<br />
0xb7d616f6 in realloc () from /lib/i686/cmov/libc.so.6</p>
</blockquote>
<p>wieso kommt hier ein segfault raus? ich gebe ihm per request_uri.c_str() ein char-array bzw. einen pointer drauf, oder? wenn ich den string direkt uebergebe bricht das programm naemlich ab, sobald ich chunk.memory ausgeben will, allerdings ohne irgendwelche meldungen, das endl kommt garnet mehr zum zuge...<br />
ich muss das programm bis dienstag mittag fertig haben, sonst haette ich noch mehr herumprobiert.<br />
wer weiss Rat? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f4a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--light_bulb"
      title=":bulb:"
      alt="💡"
    /><br />
C167</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/185342/curl-und-segfault</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 06:16:22 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/185342.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 24 Jun 2007 13:12:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to curl und segfault on Sun, 24 Jun 2007 13:16:25 GMT]]></title><description><![CDATA[<p>Tagchen,<br />
ich hab folgendes:<br />
ein Programm, das eine Internetseite in ein struct schreiben soll.<br />
der eigentliche Code, der die URL entgegen nimmt und die Seite &quot;ansurft&quot; sowie die daten in das struct schreibt ist fast 1:1 eines der Beispiele zur libcurl, nur eben in eine Klasse gequetsch.<br />
die klasse:</p>
<pre><code class="language-cpp">#include &quot;config.h&quot;
#include &quot;http.h&quot;
#include &quot;Version.h&quot;
#include &lt;curl/curl.h&gt;
#include &lt;curl/curlver.h&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstring&gt;

using namespace std;

void *HTTP::myrealloc(void *ptr, size_t size) {
        /* There might be a realloc() out there that doesn't like reallocing
         NULL pointers, so we take care of it here */
        if (ptr)
                return realloc(ptr, size);
        else
                return malloc(size);
}

size_t HTTP::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
        size_t realsize = size * nmemb;
        struct MemoryStruct *mem =( struct MemoryStruct *)data;

        mem-&gt;memory = (char *)myrealloc(mem-&gt;memory, mem-&gt;size + realsize + 1);
        if (mem-&gt;memory) {
                memcpy(&amp;(mem-&gt;memory[mem-&gt;size]), ptr, realsize);
                mem-&gt;size += realsize;
                mem-&gt;memory[mem-&gt;size] = 0;
        }
        return realsize;
}

HTTP::HTTP(const char * p_prj_url, const char * p_file, char * p_uid) {
        request_uri.append(p_prj_url);
        request_uri.append(p_file);
        request_uri.append(p_uid);

        cout &lt;&lt; &quot;prj_url: &quot; &lt;&lt; p_prj_url &lt;&lt; endl
             &lt;&lt; &quot;p_file: &quot; &lt;&lt; p_file &lt;&lt; endl
             &lt;&lt; &quot;p_uid: &quot; &lt;&lt; p_uid &lt;&lt; endl
             &lt;&lt; &quot;request_uri: &quot; &lt;&lt; request_uri &lt;&lt; endl;

        chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
        chunk.size = 0; /* no data at this point */
        cout &lt;&lt; &quot;no data at this point&quot; &lt;&lt; endl;
}

HTTP::~HTTP() {
        /* cleanup curl stuff */
        curl_easy_cleanup(curl_handle);

        if (chunk.memory) {
                free(chunk.memory);
        }
}

char * HTTP::get_code(void) {
        return chunk.memory;
}

size_t * HTTP::get_size(void) {
        return &amp;chunk.size;
}

void HTTP::performRequest(void) {
        curl_global_init(CURL_GLOBAL_ALL);

        /* init the curl session */
        curl_handle = curl_easy_init();

        /* specify URL to get */
        curl_easy_setopt(curl_handle, CURLOPT_URL, request_uri.c_str());

        /* send all data to this function  */
        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &amp;HTTP::WriteMemoryCallback);

        /* we pass our 'chunk' struct to the callback function */
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&amp;chunk);

        /* some servers don't like requests that are made without a user-agent field, so we provide one */
        cout &lt;&lt; &quot;setting user-agent: &quot; &lt;&lt; &quot;boinc_data/&quot; &lt;&lt; BOINCDATA_VERSION &lt;&lt; &quot; using libcurl/&quot; &lt;&lt; LIBCURL_VERSION &lt;&lt; endl;
        curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, &quot;boinc_data/&quot;BOINCDATA_VERSION&quot; using libcurl/&quot;LIBCURL_VERSION);

        const char * encoding = &quot;UTF-8&quot;;
        cout &lt;&lt; &quot;setting encoding: &quot; &lt;&lt; encoding &lt;&lt; endl;
        curl_easy_setopt(curl_handle, CURLOPT_ENCODING, encoding);

        curl_easy_perform(curl_handle);
        cout &lt;&lt; &quot;Data dump: &quot; &lt;&lt; chunk.memory &lt;&lt; endl
             &lt;&lt; &quot;Size: &quot; &lt;&lt; chunk.size &lt;&lt; endl;
}
</code></pre>
<p>alles was cout betrifft ist normalerweise nicht da, nur zum einfacheren debugging. Wenn ich das Programm mit den entsprechenden Parametern aufrufe, kommt das raus:</p>
<blockquote>
<p>prj_url: <a href="http://setiweb.ssl.berkeley.edu/" rel="nofollow">http://setiweb.ssl.berkeley.edu/</a><br />
p_file: show_user.php?userid=<br />
p_uid: 1234<br />
request_uri: <a href="http://setiweb.ssl.berkeley.edu/show_user.php?userid=1234" rel="nofollow">http://setiweb.ssl.berkeley.edu/show_user.php?userid=1234</a><br />
no data at this point<br />
setting user-agent: boinc_data/0.0.1 using libcurl/7.16.2<br />
setting encoding: UTF-8<br />
Speicherzugriffsfehler</p>
</blockquote>
<p>mit gdb debugged:</p>
<blockquote>
<p>prj: 28<br />
uid: 1<br />
mode: 0<br />
file: show_user.php?userid=<br />
instantiating the HTTP-Class...<br />
prj_url: <a href="http://setiweb.ssl.berkeley.edu/" rel="nofollow">http://setiweb.ssl.berkeley.edu/</a><br />
p_file: show_user.php?userid=<br />
p_uid: 1<br />
request_uri: <a href="http://setiweb.ssl.berkeley.edu/show_user.php?userid=1" rel="nofollow">http://setiweb.ssl.berkeley.edu/show_user.php?userid=1</a><br />
no data at this point<br />
setting user-agent: boinc_data/0.0.1 using libcurl/7.16.2<br />
setting encoding: UTF-8</p>
<p>Program received signal SIGSEGV, Segmentation fault.<br />
0xb7d616f6 in realloc () from /lib/i686/cmov/libc.so.6</p>
</blockquote>
<p>wieso kommt hier ein segfault raus? ich gebe ihm per request_uri.c_str() ein char-array bzw. einen pointer drauf, oder? wenn ich den string direkt uebergebe bricht das programm naemlich ab, sobald ich chunk.memory ausgeben will, allerdings ohne irgendwelche meldungen, das endl kommt garnet mehr zum zuge...<br />
ich muss das programm bis dienstag mittag fertig haben, sonst haette ich noch mehr herumprobiert.<br />
wer weiss Rat? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f4a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--light_bulb"
      title=":bulb:"
      alt="💡"
    /><br />
C167</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1312715</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1312715</guid><dc:creator><![CDATA[C167]]></dc:creator><pubDate>Sun, 24 Jun 2007 13:16:25 GMT</pubDate></item><item><title><![CDATA[Reply to curl und segfault on Tue, 03 Jul 2007 11:45:35 GMT]]></title><description><![CDATA[<p>okay, kann geschlossen werden, ich hab myrealloc sowie WriteMemoryCallback aus der Klasse rausgenommen und als normale funktionen deklariert, wie dies auch im CURL-Beispiel aufgefuehrt ist... siehe da, schon geht es</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1317444</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1317444</guid><dc:creator><![CDATA[C167]]></dc:creator><pubDate>Tue, 03 Jul 2007 11:45:35 GMT</pubDate></item></channel></rss>