<pre><code>class someclass {};

class base
{
    int a;
    int *pint;
    someclass objsomeclass;
    someclass* psomeclass;
public:
    base()
    {
        objsomeclass = someclass();
        psomeclass = new someclass();
        pint = new int(); 
        throw "constructor failed";
        a = 43;
    }
}

int main()
{
    base temp();
}
</code></pre>

<p>In the above code, the constructor throws. Which objects will be leaked, and how can the memory leaks be avoided?</p>

<pre><code>int main()
{
    base *temp = new base();
}
</code></pre>

<p>How about in the above code? How can the memory leaks be avoided after the constructor throws?</p>

Everything you "new" needs to be deleted, or you'll cause a memory leak. So these two lines:
```
```
psomeclass = new someclass();
pint = new int();
```
```
Will cause memory leaks, because you need to do:
```
```
delete pint;
delete psomeclass;
```
```
In a finally block to avoid them being leaked.
Also, this line:
```
```
base temp = base();
```
```
Is unnecessary. You just need to do:
```
```
base temp;
```
```
Adding the "= base()" is unnecessary.