Unable to exit as destructor errors cause execution of main() to halt upon exit
<p>I have narrowed my problem down to passing 2 objects (which contain pointer data members) to a simple void function. The function returns clean, but when main() attempts to exit, it can not reclaim the first of the 2 objects. Here is a sample piece of code that shows the issue - along with print statements to show the address's of the objects as they are constructed, passed, and destructed.</p>

<p>If I only call "print1" - the program runs fine. However, if I call "printboth" - then the object "myNumbers" can not be freed. I can also make the error go away by removing the destructor statement:</p>

<pre><code> delete [] number;
</code></pre>

<p>but I don't think this is a good idea.</p>

<p>Anyone have any ideas?</p>

<pre><code>class dummy
{
public:
    dummy() {
        number = new int[1];
        currentPos = -1;
        std::cout&lt;&lt;"default constructor called for "&lt;&lt;this&lt;&lt;std::endl;

    }
    dummy(int len) {
        number = new int[len];
        currentPos = -1;
        std::cout&lt;&lt;"parameterized constructor called for "&lt;&lt;this&lt;&lt;std::endl;

    }
    ~dummy() {
        cout&lt;&lt;"Calling destructor for "&lt;&lt;this&lt;&lt;endl;
        delete [] number;
    }
    int getNextNumber() {
        currentPos++;
        return number[currentPos];
    }
    void setNumbers(int position, int value) {
        number[position] = value;
    }
private:
    int* number;
    int currentPos;
};

void print1(dummy);
void printboth(dummy, dummy);

int main() {
dummy myNumbers(3);
myNumbers.setNumbers(0,0);
myNumbers.setNumbers(1,1);


dummy myOtherNumbers(3);
myOtherNumbers.setNumbers(0,4);
myOtherNumbers.setNumbers(1,5);

cout&lt;&lt;"Address of myNumbers is      "&lt;&lt;&amp;myNumbers&lt;&lt;endl;
cout&lt;&lt;"Address of myOtherNumbers is "&lt;&lt;&amp;myOtherNumbers&lt;&lt;endl;

print1(myNumbers);
printboth(myNumbers, myOtherNumbers);

system("PAUSE");
return 0;
}

void print1(dummy num) {
cout&lt;&lt;"Address of num is      "&lt;&lt;&amp;num&lt;&lt;endl;
for (int i=0;i&lt;4;i++)
    cout&lt;&lt;"Dummy number1 is "&lt;&lt;num.getNextNumber()&lt;&lt;endl;
return;
}
void printboth(dummy num1, dummy num2) {
cout&lt;&lt;"Address of num1 is      "&lt;&lt;&amp;num1&lt;&lt;endl;
cout&lt;&lt;"Address of num2 is      "&lt;&lt;&amp;num2&lt;&lt;endl;
for (int i=0;i&lt;4;i++) {
    cout&lt;&lt;"Dummy number1 is "&lt;&lt;num1.getNextNumber()&lt;&lt;endl;
    cout&lt;&lt;"Dummy number2 is "&lt;&lt;num2.getNextNumber()&lt;&lt;endl;
    }
return;
}
</code></pre>
