Hi,
I'm using VC++ 6.0 (SP 5) and noticed this behavior:
Basically it comes down to this example:

// Class with virtual destructor
class CMyClass
{
public:
CMyClass() {}
virtual ~CMyClass(){}
};

int main(int argc, char* argv[])
{
int iSize = 0; // Normally comes from some function where 0 is possible
CMyClass *poMyArray = new CMyClass [iSize];

for (int i=0; i<iSize; i++)
{
// Do some calculation...
}

delete [] poMyArray;
// --> Unhandled exception in prog.exe: 0xC0000005: Access Violation

return 0;
}

If I make the destructor non-virtual everyhing is fine.
I've looked at some previous posts around this issue and some suggested it
to be a compiler bug.
If this was a compiler bug, has this been resolved in SP6 or
is this a good reason to upgrade to Visual Studio.NET ?

Best regards,
Colin B.

Re: unhandled exception when using virtual destructor and new [0] by Doug

Doug
Wed May 12 10:17:47 CDT 2004

Colin Bouckaert wrote:

>Hi,
>I'm using VC++ 6.0 (SP 5) and noticed this behavior:
>Basically it comes down to this example:
>
>// Class with virtual destructor
>class CMyClass
>{
> public:
> CMyClass() {}
> virtual ~CMyClass(){}
>};
>
>int main(int argc, char* argv[])
>{
> int iSize = 0; // Normally comes from some function where 0 is possible
> CMyClass *poMyArray = new CMyClass [iSize];
>
> for (int i=0; i<iSize; i++)
> {
> // Do some calculation...
> }
>
> delete [] poMyArray;
> // --> Unhandled exception in prog.exe: 0xC0000005: Access Violation
>
> return 0;
>}
>
>If I make the destructor non-virtual everyhing is fine.
>I've looked at some previous posts around this issue and some suggested it
>to be a compiler bug.
>If this was a compiler bug, has this been resolved in SP6 or
>is this a good reason to upgrade to Visual Studio.NET ?

Known bug, appears to be fixed in VC.NET.

--
Doug Harrison
Microsoft MVP - Visual C++

Re: unhandled exception when using virtual destructor and new [0] by Colin

Colin
Thu May 13 04:33:48 CDT 2004

"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:grf4a09lrmm4heb8i3q7j7glbqttd967pd@4ax.com...
>
> Known bug, appears to be fixed in VC.NET.
>
> --
> Doug Harrison
> Microsoft MVP - Visual C++

OK, thanks.
I will set the pointer to NULL if its size is 0.
The upgrade should be made somewhere in the future.

Colin B.



Re: unhandled exception when using virtual destructor and new [0] by Doug

Doug
Thu May 13 12:11:33 CDT 2004

Colin Bouckaert wrote:

>OK, thanks.
>I will set the pointer to NULL if its size is 0.
>The upgrade should be made somewhere in the future.

In addition, you might consider using std::vector.

--
Doug Harrison
Microsoft MVP - Visual C++