C++ std::sort() Calling Destructor
<p>I overloaded my class' <em>()</em> operator to use it as a sort comparer function. When using <em>std::sort()</em>, it for some reason calls the the class' destructor a bunch of times (dependant on the amount of entries in the vector, apparently). I've described more in ~RANK().</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

class RANK
{
    struct COMBO
    {
        int x;
    };

    std::vector&lt;COMBO *&gt; data;
public:
    RANK()
    {
        printf("RANK()\n");
    }

    ~RANK()
    {
        printf("~RANK()\n");

        /*
         * Here is the problem.
         * Since my vector consists of pointers to COMBO objects,
         * I delete them upon RANK object's destruction. However,
         * std::sort() calls RANK's destructor many times and
         * throws some runtime error, unless commented out.
         */
        //for (unsigned int i = 0, n = data.size(); i &lt; n; i++)
        //  delete data[i];
    }

    void Add(int x)
    {
        COMBO *combo = new COMBO();
        combo-&gt;x = x;

        data.push_back(combo);
    }

    unsigned int Size()
    {
        return data.size();
    }

    void Sort()
    {
        std::sort(data.begin(), data.end(), *this);
    }

    int operator[](unsigned int pos)
    {
        return data[pos]-&gt;x;
    }

    bool operator()(COMBO *combo1, COMBO *combo2)
    {
        return combo1-&gt;x &gt; combo2-&gt;x;
    }
};

int main()
{
    RANK rank;
    rank.Add(1337);
    rank.Add(9001);
    rank.Sort();

    for (unsigned int i = 0, n = rank.Size(); i &lt; n; i++)
        printf("%d ", rank[i]);
        printf("\n");

    system("pause");

    return 0;
}
</code></pre>

<p>The output (with commented destructor):</p>

<pre><code>RANK()
~RANK()
~RANK()
~RANK()
~RANK()
~RANK()
9001 1337
</code></pre>
