<?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[Segmentation fault: 11 bei delete]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich bekomme bei einem Aufruf von delete einen Programmabbruch mit der Fehlermeldung &quot;Segmentation fault: 11&quot;. Eine kurze Recherche zu dem Fehler bringt mich zu der bedeutung &quot;Signal 11, or officially know as &quot;segmentation fault&quot;, means that the program accessed a memory location that was not assigned.&quot;</p>
<p>Leider kann ich die Stelle bei der ich auf nicht assignedten Speicher zugreife nicht erkennen.</p>
<p>auszug <a href="http://ATSTelephonyObserver.mm" rel="nofollow">ATSTelephonyObserver.mm</a>:</p>
<pre><code class="language-cpp">std::vector&lt;ATSCmd*&gt; cmdVector;

ATSSms* readSMS(CFDictionaryRef userInfo){
  ...
  return new ATSSms(senderNumber.UTF8String,smsText.UTF8String);
}

void ATSTelephonyObserver::callback(CFStringRef name){
  NSString * nsname = ( NSString *)name;
  if([nsname isEqualToString:@&quot;kCTMessageReceivedNotification&quot;]){
    ATSSms* sms = readSMS(userInfo);
    try{
      ATSCmd*  cmd = sms-&gt;toATSCmd(self-&gt;satsData);
      cmdVector.push_back(cmd);
    }catch(char const* ch){
      std::cout &lt;&lt; &quot;sms keine atsSMS weil: &quot; &lt;&lt; ch;
    }
    delete sms;
  }else if([nsname isEqualToString:@&quot;kCTRegistrationNetworkSelectedNotification&quot;]){
    while(cmdVector.back()!=0){
      ATSCmd* cmd = cmdVector.back();
      cmd-&gt;proceed();
      cmdVector.pop_back();
      delete cmd; //&lt;---------- hier kommt der &quot;Segmentation fault: 11&quot;
    }
  }
}
</code></pre>
<p>ATSSms.h:</p>
<pre><code class="language-cpp">#ifndef __ATSSmsCmd_H_
#define __ATSSmsCmd_H_

#include &lt;iostream&gt;
#include &quot;ATSData.h&quot;
#include &quot;ATSCmd.h&quot;

class ATSSms {
    public:
        ATSSms(std::string number, std::string txt){
            this-&gt;number = number;
            this-&gt;txt = txt;
        };
        std::string getNumber(){return this-&gt;number;};
        std::string getText(){return this-&gt;txt;};
        ATSCmd* toATSCmd(ATSData &amp;atsData);
    private:
        std::string number;
        std::string txt;
};

#endif //__ATSSmsCmd_H_
</code></pre>
<p><a href="http://ATSSms.mm" rel="nofollow">ATSSms.mm</a>:</p>
<pre><code class="language-cpp">#include &quot;../headers/ATSSms.h&quot;
#include &quot;../headers/functions.h&quot;

ATSCmd* ATSSms::toATSCmd(ATSData &amp;atsData) {

    std::string txt = this-&gt;txt;
    std::string val = &quot;:&quot;;

    int dpcount = countPartStr(txt, val);
    if (!(dpcount &gt;= 2)) {
        throw &quot;double dot count &lt; 2&quot;;
    }
    std::string part = txt.substr(0, 4);
    if ((part.compare(&quot;ats:&quot;))) {
        throw &quot;no ats sms&quot;;
    }
    int pos = txt.find(val, 4);
    part = txt.substr(4, pos - 4);
    if((part.compare(&quot;1234&quot;))){
        throw &quot;ats-command password wrong&quot;;
    }
    if ((dpcount == 2)) {
        part = txt.substr(pos+1);
    }else{
        int pos2 = txt.find(val, pos+1);
        part = txt.substr(pos+1, pos2-pos-1);
    }
    if(part.length()&lt;1){
        throw &quot;ats-command-string lenth 0&quot;;
    }

    //Auswertung:
    if(!(part.compare(&quot;locate&quot;))){
        return new ATSSendLocationSmsCmd(this-&gt;number);
    } else if(!(part.compare(&quot;lock&quot;))) {
        return new ATSLockDevice();
    } else {
        throw &quot;ats-command-string unknown&quot;;
    }
}
</code></pre>
<p>ATSCmd.h</p>
<pre><code class="language-cpp">#ifndef __ATSCmd_H_
#define __ATSCmd_H_

#include &lt;iostream&gt;
#include &lt;CoreTelephony/CTMessageCenter.h&gt;

/*abstract*/
class ATSCmd {
public:
    virtual void proceed()=0;
};

class ATSSendSmsCmd : public ATSCmd{
public:
    ATSSendSmsCmd(std::string number,std::string txt){
        this-&gt;number = number;
        this-&gt;txt = txt;
    };
    void proceed();
    private:
    std::string number;
    std::string txt;
};

class ATSSendLocationSmsCmd : public ATSCmd {
    public:
        ATSSendLocationSmsCmd(std::string number){this-&gt;number = number;};
        void proceed();
    private:
        std::string number;
};

class ATSLockDevice : public ATSCmd {
    public:
        ATSLockDevice():ATSCmd(){};
        void proceed();
};

#endif //__ATSCmd_H_
</code></pre>
<p>Wenn ich die Zeile &quot;delete cmd;&quot; herrausnehme tritt kein Fehler auf, aber habe ich dann nicht ein memory-leak?</p>
<p>PS: Es handelt sich um objective c++ code für iOS.</p>
<p>lg, Phill</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/309817/segmentation-fault-11-bei-delete</link><generator>RSS for Node</generator><lastBuildDate>Wed, 05 Aug 2026 01:52:17 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/309817.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 28 Oct 2012 10:19:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Segmentation fault: 11 bei delete on Sun, 28 Oct 2012 10:19:22 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich bekomme bei einem Aufruf von delete einen Programmabbruch mit der Fehlermeldung &quot;Segmentation fault: 11&quot;. Eine kurze Recherche zu dem Fehler bringt mich zu der bedeutung &quot;Signal 11, or officially know as &quot;segmentation fault&quot;, means that the program accessed a memory location that was not assigned.&quot;</p>
<p>Leider kann ich die Stelle bei der ich auf nicht assignedten Speicher zugreife nicht erkennen.</p>
<p>auszug <a href="http://ATSTelephonyObserver.mm" rel="nofollow">ATSTelephonyObserver.mm</a>:</p>
<pre><code class="language-cpp">std::vector&lt;ATSCmd*&gt; cmdVector;

ATSSms* readSMS(CFDictionaryRef userInfo){
  ...
  return new ATSSms(senderNumber.UTF8String,smsText.UTF8String);
}

void ATSTelephonyObserver::callback(CFStringRef name){
  NSString * nsname = ( NSString *)name;
  if([nsname isEqualToString:@&quot;kCTMessageReceivedNotification&quot;]){
    ATSSms* sms = readSMS(userInfo);
    try{
      ATSCmd*  cmd = sms-&gt;toATSCmd(self-&gt;satsData);
      cmdVector.push_back(cmd);
    }catch(char const* ch){
      std::cout &lt;&lt; &quot;sms keine atsSMS weil: &quot; &lt;&lt; ch;
    }
    delete sms;
  }else if([nsname isEqualToString:@&quot;kCTRegistrationNetworkSelectedNotification&quot;]){
    while(cmdVector.back()!=0){
      ATSCmd* cmd = cmdVector.back();
      cmd-&gt;proceed();
      cmdVector.pop_back();
      delete cmd; //&lt;---------- hier kommt der &quot;Segmentation fault: 11&quot;
    }
  }
}
</code></pre>
<p>ATSSms.h:</p>
<pre><code class="language-cpp">#ifndef __ATSSmsCmd_H_
#define __ATSSmsCmd_H_

#include &lt;iostream&gt;
#include &quot;ATSData.h&quot;
#include &quot;ATSCmd.h&quot;

class ATSSms {
    public:
        ATSSms(std::string number, std::string txt){
            this-&gt;number = number;
            this-&gt;txt = txt;
        };
        std::string getNumber(){return this-&gt;number;};
        std::string getText(){return this-&gt;txt;};
        ATSCmd* toATSCmd(ATSData &amp;atsData);
    private:
        std::string number;
        std::string txt;
};

#endif //__ATSSmsCmd_H_
</code></pre>
<p><a href="http://ATSSms.mm" rel="nofollow">ATSSms.mm</a>:</p>
<pre><code class="language-cpp">#include &quot;../headers/ATSSms.h&quot;
#include &quot;../headers/functions.h&quot;

ATSCmd* ATSSms::toATSCmd(ATSData &amp;atsData) {

    std::string txt = this-&gt;txt;
    std::string val = &quot;:&quot;;

    int dpcount = countPartStr(txt, val);
    if (!(dpcount &gt;= 2)) {
        throw &quot;double dot count &lt; 2&quot;;
    }
    std::string part = txt.substr(0, 4);
    if ((part.compare(&quot;ats:&quot;))) {
        throw &quot;no ats sms&quot;;
    }
    int pos = txt.find(val, 4);
    part = txt.substr(4, pos - 4);
    if((part.compare(&quot;1234&quot;))){
        throw &quot;ats-command password wrong&quot;;
    }
    if ((dpcount == 2)) {
        part = txt.substr(pos+1);
    }else{
        int pos2 = txt.find(val, pos+1);
        part = txt.substr(pos+1, pos2-pos-1);
    }
    if(part.length()&lt;1){
        throw &quot;ats-command-string lenth 0&quot;;
    }

    //Auswertung:
    if(!(part.compare(&quot;locate&quot;))){
        return new ATSSendLocationSmsCmd(this-&gt;number);
    } else if(!(part.compare(&quot;lock&quot;))) {
        return new ATSLockDevice();
    } else {
        throw &quot;ats-command-string unknown&quot;;
    }
}
</code></pre>
<p>ATSCmd.h</p>
<pre><code class="language-cpp">#ifndef __ATSCmd_H_
#define __ATSCmd_H_

#include &lt;iostream&gt;
#include &lt;CoreTelephony/CTMessageCenter.h&gt;

/*abstract*/
class ATSCmd {
public:
    virtual void proceed()=0;
};

class ATSSendSmsCmd : public ATSCmd{
public:
    ATSSendSmsCmd(std::string number,std::string txt){
        this-&gt;number = number;
        this-&gt;txt = txt;
    };
    void proceed();
    private:
    std::string number;
    std::string txt;
};

class ATSSendLocationSmsCmd : public ATSCmd {
    public:
        ATSSendLocationSmsCmd(std::string number){this-&gt;number = number;};
        void proceed();
    private:
        std::string number;
};

class ATSLockDevice : public ATSCmd {
    public:
        ATSLockDevice():ATSCmd(){};
        void proceed();
};

#endif //__ATSCmd_H_
</code></pre>
<p>Wenn ich die Zeile &quot;delete cmd;&quot; herrausnehme tritt kein Fehler auf, aber habe ich dann nicht ein memory-leak?</p>
<p>PS: Es handelt sich um objective c++ code für iOS.</p>
<p>lg, Phill</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2264847</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2264847</guid><dc:creator><![CDATA[Soahc]]></dc:creator><pubDate>Sun, 28 Oct 2012 10:19:22 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault: 11 bei delete on Sun, 28 Oct 2012 10:25:13 GMT]]></title><description><![CDATA[<p>Edit: Habe dein P.S.: Nicht gesehen.<br />
Aber trotzdem, falsches Forum, wieso gehst du nicht in ein Forum deiner Programmiersprache?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2264850</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2264850</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 28 Oct 2012 10:25:13 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault: 11 bei delete on Sun, 28 Oct 2012 10:31:13 GMT]]></title><description><![CDATA[<p>du machst ein delete auf einen zeiger der basis klasse (welche du als ein Interface verwendest).<br />
Dadurch wird nicht das komplette objekt zerstört, da du keinen virtuellen destruktor in der basis klasse definiert hast.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2264852</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2264852</guid><dc:creator><![CDATA[firefly]]></dc:creator><pubDate>Sun, 28 Oct 2012 10:31:13 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault: 11 bei delete on Sun, 28 Oct 2012 10:32:56 GMT]]></title><description><![CDATA[<p>Soahc schrieb:</p>
<blockquote>
<pre><code class="language-cpp">std::vector&lt;ATSCmd*&gt; cmdVector;
/// [...]
    while(cmdVector.back()!=0){
      ATSCmd* cmd = cmdVector.back();
      cmd-&gt;proceed();
      cmdVector.pop_back();
      delete cmd; //&lt;---------- hier kommt der &quot;Segmentation fault: 11&quot;
    }
  }
}
</code></pre>
</blockquote>
<p>Was ist denn das für eine Laufbedingung? Wäre es nicht interessanter zu wissen, ob noch Elemente im Vektor sind?!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2264853</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2264853</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Sun, 28 Oct 2012 10:32:56 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault: 11 bei delete on Sun, 28 Oct 2012 13:47:40 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">while(cmdVector.back()!=0)
</code></pre>
<p>Du machst also folgendes:<br />
-&gt; prüfen ob der letzte Zeiger im Vector ein Nullpointer ist<br />
-&gt; wenn nicht das letzte Element aus dem Vector entfernen</p>
<p>Und was passiert wohl, wenn kein Nullpointer im vector drin ist? Ich weiß nicht genau, was für einen Zeiger back() dann zurück liefert, es ist wahrscheinlich einer, der auf Speicherbereich zeigt, der dir nicht gehört.</p>
<p>Benutz doch einfach <code>for_each</code> und einen <code>reverse_iterator</code> . Oder nimm die gute alte <code>for</code> -Schleife.</p>
<blockquote>
<p>Aber trotzdem, falsches Forum, wieso gehst du nicht in ein Forum deiner Programmiersprache?</p>
</blockquote>
<p>Mal wieder sehr nützlich, weil sich sein Problem auch gar nicht um C++ dreht ist er im falschen Forum. Die Frage kann man, also jeder außer dir, auch ohne Objective C++-Kenntnisse beantworten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2264933</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2264933</guid><dc:creator><![CDATA[Der Tobi]]></dc:creator><pubDate>Sun, 28 Oct 2012 13:47:40 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault: 11 bei delete on Sun, 28 Oct 2012 19:07:58 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Aber trotzdem, falsches Forum, wieso gehst du nicht in ein Forum deiner Programmiersprache?</p>
</blockquote>
<p>Bei objective c++ kann man sowohl c++ als auch objective c verwenden und auch unter bestimmten Bedingungen mischen. De Abgesehen von ein paar Funtkionsaufrufen und Datentypen ist kaum Objective-C code dabei. Ich habe die Frage hier gepostet, da sich die von mir vermutete Fehlerquelle ausschließlich auf c++ code bezieht.</p>
<p>firefly schrieb:</p>
<blockquote>
<p>du machst ein delete auf einen zeiger der basis klasse (welche du als ein Interface verwendest).<br />
Dadurch wird nicht das komplette objekt zerstört, da du keinen virtuellen destruktor in der basis klasse definiert hast.</p>
</blockquote>
<p>du hast mich ertappt. ich komme eigentlich aus der java-fraktion. Ich wollte die basisklasse als eine art interface verwenden und wusste nicht, dass dann der destruktor virtuell sein muss. Mir war nicht mal klar, dass man hier überhaupt einen Destruktoe benötigt, weil die klassen ja keine dynamisch erzeugten Objekte bzw. angeforderten Speicher in form von Zeigern verwalten. Eine kleine Frage dazu. Wenn ich in der Basisklasse den Destruktor virtuell mache, muss er dann auch in allen abgeleitetten Klassen virtuell sein? Also auch wenn sich am Destruktor selber nichts verändern würde.</p>
<p>Der Tobi schrieb:</p>
<blockquote>
<p>Du machst also folgendes:<br />
-&gt; prüfen ob der letzte Zeiger im Vector ein Nullpointer ist<br />
-&gt; wenn nicht das letzte Element aus dem Vector entfernen</p>
<p>Und was passiert wohl, wenn kein Nullpointer im vector drin ist? Ich weiß nicht genau, was für einen Zeiger back() dann zurück liefert, es ist wahrscheinlich einer, der auf Speicherbereich zeigt, der dir nicht gehört.</p>
<p>Benutz doch einfach <code>for_each</code> und einen <code>reverse_iterator</code> . Oder nimm die gute alte <code>for</code> -Schleife.</p>
</blockquote>
<p>Ich habe bisher leider nur sehr wenig mit der STL gearbeitet und muss da wohl noch einiges lernen. ich hatte mir die Referenz der Back-Methode des Vectors unter <a href="http://www.cplusplus.com/reference/stl/vector/back/" rel="nofollow">http://www.cplusplus.com/reference/stl/vector/back/</a> angesehen und dort wird als Beispiel das gezeigt, was teil meines codes ist.<br />
Aber danke für den Tip, ich werde den code gleich anpassen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>lg Phill</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2265075</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2265075</guid><dc:creator><![CDATA[Soahc]]></dc:creator><pubDate>Sun, 28 Oct 2012 19:07:58 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault: 11 bei delete on Sun, 28 Oct 2012 20:58:26 GMT]]></title><description><![CDATA[<p>Soahc schrieb:</p>
<blockquote>
<p>du hast mich ertappt. ich komme eigentlich aus der java-fraktion. Ich wollte die basisklasse als eine art interface verwenden und wusste nicht, dass dann der destruktor virtuell sein muss. Mir war nicht mal klar, dass man hier überhaupt einen Destruktoe benötigt, weil die klassen ja keine dynamisch erzeugten Objekte bzw. angeforderten Speicher in form von Zeigern verwalten. Eine kleine Frage dazu. Wenn ich in der Basisklasse den Destruktor virtuell mache, muss er dann auch in allen abgeleitetten Klassen virtuell sein? Also auch wenn sich am Destruktor selber nichts verändern würde.</p>
</blockquote>
<p>Die destruktoren der abgeleiteten Klassen sind automatisch auch virtual, wenn der Destruktor der Basis virtuell ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2265119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2265119</guid><dc:creator><![CDATA[firefly]]></dc:creator><pubDate>Sun, 28 Oct 2012 20:58:26 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault: 11 bei delete on Sun, 28 Oct 2012 21:09:59 GMT]]></title><description><![CDATA[<p>Und wenn Du ein vererbtes Objekt über den Basisklassenzeiger (statischer Typ == Basisklasse) löscht, dann brauchst Du eben auch dann einen virtuellen destructor, wenn es keine dynamisch verwalteten Elemente gibt - um Deine Erkenntnis nochmal zu bestätigen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2265127</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2265127</guid><dc:creator><![CDATA[Eisflamme]]></dc:creator><pubDate>Sun, 28 Oct 2012 21:09:59 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault: 11 bei delete on Mon, 29 Oct 2012 11:01:34 GMT]]></title><description><![CDATA[<p>Soahc schrieb:</p>
<blockquote>
<p>Ich habe bisher leider nur sehr wenig mit der STL gearbeitet und muss da wohl noch einiges lernen. ich hatte mir die Referenz der Back-Methode des Vectors unter <a href="http://www.cplusplus.com/reference/stl/vector/back/" rel="nofollow">http://www.cplusplus.com/reference/stl/vector/back/</a> angesehen und dort wird als Beispiel das gezeigt, was teil meines codes ist.</p>
</blockquote>
<p>Dort passiert mit dem Vector aber etwas völlig anderes als bei dir. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Im Beispiel wird der Vector durch die Schleife stets vergrößert bis halt irgendwann &quot;0&quot; als letztes Element enthalten ist. Also ist &quot;myvector.back()&quot; auch stets ein valider Aufruf.</p>
<p>Bei dir wird der Vector mit jeder Iteration verkleinert (gedacht ist wohl bis keine Elemente mehr enthalten sind). Wenn der Vector leer ist, ist &quot;cmdVector.back()&quot; nicht mehr valide, es gibt ja kein letztes Element. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";-)"
      alt="😉"
    /></p>
<p>Abgesehen von &quot;for&quot; oder &quot;for_each&quot; kannst du natürlich auch einfach die Bedingung der while-Schleife ändern in:</p>
<pre><code class="language-cpp">while (!cmdVector.empty()) {
    // ...
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2265256</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2265256</guid><dc:creator><![CDATA[mantiz]]></dc:creator><pubDate>Mon, 29 Oct 2012 11:01:34 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault: 11 bei delete on Mon, 29 Oct 2012 11:22:29 GMT]]></title><description><![CDATA[<p>Vielen Dank für die Tips und Erklärungen... das Programm läuft inzwischen ohne Fehler durch. Ich bin wirklich erstaunt über die Hilfsbereitschaft der Mitglieder dieses Forums.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2265263</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2265263</guid><dc:creator><![CDATA[Soahc]]></dc:creator><pubDate>Mon, 29 Oct 2012 11:22:29 GMT</pubDate></item></channel></rss>