delete operator causing assertion failure 
Author Message
 delete operator causing assertion failure

Attempting to dynamically allocate memory for an array of
structures. Unfortunately, assertion failure occurs when
attempting to deallocate.

When delete operator is called, assertion failure
indicates problem in dbgheap.c., where all the debug heap-
handling functions exist and when the ASSERT macro is
used, it of course, catchs the problem w/ delete.

I dynamically allocate the array in a constructor or user-
defined function, then another function, I fill the array
and then attempt to deallocate the array.

Here's a sample:

typedef struct mystructtag
{
  _bstr_t Name;
  _variant_t Value;

Quote:
} Parms_typ;

Parms_typ* m_arrParms;

void myfunction1)
{
m_arrParms = new Parms_typ[];
or, m_arrParms = new Parms_typ[13];

Quote:
}

void myfunction2()
{
for(int i = 1; i <= arrSIZE; i++)
{
  m_arrParms[i].Name = someName;
  m_arrParms[i].Value = someValue;

Quote:
}

void myfunction3()
{
 // do stuff w/ array....
 delete[] m_arrParms; m_arrParms = 0;  // assertion
failure occurs
 ASSERT(!m_arrParms);
Quote:
}



Tue, 14 Dec 2004 10:37:14 GMT  
 delete operator causing assertion failure
This type of assertion usually means you're writing off the end of the
array - check all your subscript expressions carefully.

Another thing -

void myfunction1)
{

m_arrParms = new Parms_typ[];

// ^^^ is illegal - there's no such thing as an unsized array on the heap in
C++

Quote:
}

-cd


Quote:
> Attempting to dynamically allocate memory for an array of
> structures. Unfortunately, assertion failure occurs when
> attempting to deallocate.

> When delete operator is called, assertion failure
> indicates problem in dbgheap.c., where all the debug heap-
> handling functions exist and when the ASSERT macro is
> used, it of course, catchs the problem w/ delete.

> I dynamically allocate the array in a constructor or user-
> defined function, then another function, I fill the array
> and then attempt to deallocate the array.

> Here's a sample:

> typedef struct mystructtag
> {
>   _bstr_t Name;
>   _variant_t Value;
> } Parms_typ;

> Parms_typ* m_arrParms;

> void myfunction1)
> {
> m_arrParms = new Parms_typ[];
> or, m_arrParms = new Parms_typ[13];
> }

> void myfunction2()
> {
> for(int i = 1; i <= arrSIZE; i++)
> {
>   m_arrParms[i].Name = someName;
>   m_arrParms[i].Value = someValue;
> }

> void myfunction3()
> {
>  // do stuff w/ array....
>  delete[] m_arrParms; m_arrParms = 0;  // assertion
> failure occurs
>  ASSERT(!m_arrParms);
> }



Tue, 14 Dec 2004 11:07:54 GMT  
 delete operator causing assertion failure
I have tryed your code, I do not know how you define  '_bstr_t ' and
'_variant_t'. So I define then as char[10]. Then there is no problem. I
think error must be in '_bstr_t ' and '_variant_t'. That is only my
suggest.Maybe it will help.


Tue, 14 Dec 2004 11:15:27 GMT  
 delete operator causing assertion failure
I think he is right.This is my test code.
typedef struct mystructtag
{
  char Name[10];
  char Value[10];

Quote:
} Parms_typ;

Parms_typ* m_arrParms;

void myfunction1()
{
 m_arrParms = new Parms_typ[10];
 //or, m_arrParms = new Parms_typ[13];

Quote:
}

void myfunction2()
{
 for(int i = 0; i <= 9; i++)
 {
  strcpy( m_arrParms[i].Name , "someName0");
  strcpy(m_arrParms[i].Value , "someValue");
 }
Quote:
}

void myfunction3()
{
 // do stuff w/ array....
 delete[] m_arrParms; m_arrParms = 0;  // assertion
 //failure occurs
 ASSERT(!m_arrParms);

Quote:
}



Quote:
> This type of assertion usually means you're writing off the end of the
> array - check all your subscript expressions carefully.

> Another thing -

> void myfunction1)
> {

> m_arrParms = new Parms_typ[];

> // ^^^ is illegal - there's no such thing as an unsized array on the heap
in
> C++

> }

> -cd



> > Attempting to dynamically allocate memory for an array of
> > structures. Unfortunately, assertion failure occurs when
> > attempting to deallocate.

> > When delete operator is called, assertion failure
> > indicates problem in dbgheap.c., where all the debug heap-
> > handling functions exist and when the ASSERT macro is
> > used, it of course, catchs the problem w/ delete.

> > I dynamically allocate the array in a constructor or user-
> > defined function, then another function, I fill the array
> > and then attempt to deallocate the array.

> > Here's a sample:

> > typedef struct mystructtag
> > {
> >   _bstr_t Name;
> >   _variant_t Value;
> > } Parms_typ;

> > Parms_typ* m_arrParms;

> > void myfunction1)
> > {
> > m_arrParms = new Parms_typ[];
> > or, m_arrParms = new Parms_typ[13];
> > }

> > void myfunction2()
> > {
> > for(int i = 1; i <= arrSIZE; i++)
> > {
> >   m_arrParms[i].Name = someName;
> >   m_arrParms[i].Value = someValue;
> > }

> > void myfunction3()
> > {
> >  // do stuff w/ array....
> >  delete[] m_arrParms; m_arrParms = 0;  // assertion
> > failure occurs
> >  ASSERT(!m_arrParms);
> > }



Tue, 14 Dec 2004 11:24:10 GMT  
 delete operator causing assertion failure

Quote:
> void myfunction3()
> {
>  // do stuff w/ array....
>  delete[] m_arrParms; m_arrParms = 0;  // assertion
> failure occurs
>  ASSERT(!m_arrParms);
> }

Check if m_arrParms is NULL before you delete it.


Tue, 14 Dec 2004 17:43:14 GMT  
 delete operator causing assertion failure

Quote:
> > void myfunction3()
> > {
> >  // do stuff w/ array....
> >  delete[] m_arrParms; m_arrParms = 0;  // assertion
> > failure occurs
> >  ASSERT(!m_arrParms);
> > }

> Check if m_arrParms is NULL before you delete it.

Passing NULL to delete (or dlete[] as the case may be) is specifically
legal - it has no effect.  This is in contrast to free(), where passing NULL
is undefined (and actually crashes on many systems).

-cd



Tue, 14 Dec 2004 21:04:57 GMT  
 delete operator causing assertion failure

Quote:

> I have tryed your code, I do not know how you define  '_bstr_t ' and
> '_variant_t'.

They're COM support classes.  I think they're in <comip.h>.


Tue, 14 Dec 2004 21:22:03 GMT  
 delete operator causing assertion failure
Yep. His loop is going from 1 to arraysize. Should be going from 0 to
(arraysize-1)


Quote:
> This type of assertion usually means you're writing off the end of the
> array - check all your subscript expressions carefully.

> Another thing -

> void myfunction1)
> {

> m_arrParms = new Parms_typ[];

> // ^^^ is illegal - there's no such thing as an unsized array on the heap
in
> C++

> }

> -cd



> > Attempting to dynamically allocate memory for an array of
> > structures. Unfortunately, assertion failure occurs when
> > attempting to deallocate.

> > When delete operator is called, assertion failure
> > indicates problem in dbgheap.c., where all the debug heap-
> > handling functions exist and when the ASSERT macro is
> > used, it of course, catchs the problem w/ delete.

> > I dynamically allocate the array in a constructor or user-
> > defined function, then another function, I fill the array
> > and then attempt to deallocate the array.

> > Here's a sample:

> > typedef struct mystructtag
> > {
> >   _bstr_t Name;
> >   _variant_t Value;
> > } Parms_typ;

> > Parms_typ* m_arrParms;

> > void myfunction1)
> > {
> > m_arrParms = new Parms_typ[];
> > or, m_arrParms = new Parms_typ[13];
> > }

> > void myfunction2()
> > {
> > for(int i = 1; i <= arrSIZE; i++)
> > {
> >   m_arrParms[i].Name = someName;
> >   m_arrParms[i].Value = someValue;
> > }

> > void myfunction3()
> > {
> >  // do stuff w/ array....
> >  delete[] m_arrParms; m_arrParms = 0;  // assertion
> > failure occurs
> >  ASSERT(!m_arrParms);
> > }



Tue, 14 Dec 2004 23:27:50 GMT  
 delete operator causing assertion failure
Yes, I thought the same thing. However, if I dynamically
allocate in a constructor, there's no problem w/
m_arrParms = new Parms_typ[];

If done allocated in other than a constructor, an error,
of course occurs.

I apologize for the misleading code.

I will definitely consider the suggestion you made.
thanks.

jim

Quote:
>-----Original Message-----
>This type of assertion usually means you're writing off
the end of the
>array - check all your subscript expressions carefully.

>Another thing -

>void myfunction1)
>{

>m_arrParms = new Parms_typ[];

>// ^^^ is illegal - there's no such thing as an unsized

array on the heap in
Quote:
>C++

>}

>-cd



>> Attempting to dynamically allocate memory for an array
of
>> structures. Unfortunately, assertion failure occurs when
>> attempting to deallocate.

>> When delete operator is called, assertion failure
>> indicates problem in dbgheap.c., where all the debug
heap-
>> handling functions exist and when the ASSERT macro is
>> used, it of course, catchs the problem w/ delete.

>> I dynamically allocate the array in a constructor or
user-
>> defined function, then another function, I fill the
array
>> and then attempt to deallocate the array.

>> Here's a sample:

>> typedef struct mystructtag
>> {
>>   _bstr_t Name;
>>   _variant_t Value;
>> } Parms_typ;

>> Parms_typ* m_arrParms;

>> void myfunction1)
>> {
>> m_arrParms = new Parms_typ[];
>> or, m_arrParms = new Parms_typ[13];
>> }

>> void myfunction2()
>> {
>> for(int i = 1; i <= arrSIZE; i++)
>> {
>>   m_arrParms[i].Name = someName;
>>   m_arrParms[i].Value = someValue;
>> }

>> void myfunction3()
>> {
>>  // do stuff w/ array....
>>  delete[] m_arrParms; m_arrParms = 0;  // assertion
>> failure occurs
>>  ASSERT(!m_arrParms);
>> }

>.



Wed, 15 Dec 2004 02:47:18 GMT  
 delete operator causing assertion failure
Must include comdef.h to use COM classes
#include <comdef.h>

Quote:
>-----Original Message-----

>> I have tryed your code, I do not know how you

define  '_bstr_t ' and
Quote:
>> '_variant_t'.

>They're COM support classes.  I think they're in
<comip.h>.
>.



Wed, 15 Dec 2004 02:50:08 GMT  
 delete operator causing assertion failure
Must include comdef.h to use COM classes
#include <comdef.h>

Quote:
>-----Original Message-----
>I have tryed your code, I do not know how you

define  '_bstr_t ' and
Quote:
>'_variant_t'. So I define then as char[10]. Then there is
no problem. I
>think error must be in '_bstr_t ' and '_variant_t'. That
is only my
>suggest.Maybe it will help.

>.



Wed, 15 Dec 2004 02:50:34 GMT  
 delete operator causing assertion failure
I think you are absolutely on the money. My bad.

thanks.

jim

Quote:
>-----Original Message-----
>Yep. His loop is going from 1 to arraysize. Should be
going from 0 to
>(arraysize-1)



>> This type of assertion usually means you're writing off
the end of the
>> array - check all your subscript expressions carefully.

>> Another thing -

>> void myfunction1)
>> {

>> m_arrParms = new Parms_typ[];

>> // ^^^ is illegal - there's no such thing as an unsized
array on the heap
>in
>> C++

>> }

>> -cd



>> > Attempting to dynamically allocate memory for an
array of
>> > structures. Unfortunately, assertion failure occurs
when
>> > attempting to deallocate.

>> > When delete operator is called, assertion failure
>> > indicates problem in dbgheap.c., where all the debug
heap-
>> > handling functions exist and when the ASSERT macro is
>> > used, it of course, catchs the problem w/ delete.

>> > I dynamically allocate the array in a constructor or
user-
>> > defined function, then another function, I fill the
array
>> > and then attempt to deallocate the array.

>> > Here's a sample:

>> > typedef struct mystructtag
>> > {
>> >   _bstr_t Name;
>> >   _variant_t Value;
>> > } Parms_typ;

>> > Parms_typ* m_arrParms;

>> > void myfunction1)
>> > {
>> > m_arrParms = new Parms_typ[];
>> > or, m_arrParms = new Parms_typ[13];
>> > }

>> > void myfunction2()
>> > {
>> > for(int i = 1; i <= arrSIZE; i++)
>> > {
>> >   m_arrParms[i].Name = someName;
>> >   m_arrParms[i].Value = someValue;
>> > }

>> > void myfunction3()
>> > {
>> >  // do stuff w/ array....
>> >  delete[] m_arrParms; m_arrParms = 0;  // assertion
>> > failure occurs
>> >  ASSERT(!m_arrParms);
>> > }

>.



Wed, 15 Dec 2004 02:52:01 GMT  
 delete operator causing assertion failure

Quote:

>Passing NULL to delete (or dlete[] as the case may be) is specifically
>legal - it has no effect.  This is in contrast to free(), where passing NULL
>is undefined (and actually crashes on many systems).

Standard C (C89) defined free(NULL) to do nothing.

--
Doug Harrison
Microsoft MVP - Visual C++
Eluent Software, LLC
http://www.eluent.com
Tools for VC++, VS.NET, and Windows



Wed, 15 Dec 2004 10:59:04 GMT  
 delete operator causing assertion failure
        Assertions are caused by some verifications on debug versions of
microsoft libraries. It's possible to see the line that failed if the
CRT source was installed.
Quote:


>>Passing NULL to delete (or dlete[] as the case may be) is specifically
>>legal - it has no effect.  This is in contrast to free(), where passing NULL
>>is undefined (and actually crashes on many systems).

> Standard C (C89) defined free(NULL) to do nothing.



Thu, 16 Dec 2004 14:29:08 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. CRecortset::Delete causes assertion failure!

2. assertion failure w/ delete operator

3. Why does this function cause an assertion failure?

4. Accessing ComboBox in dialog box causes an Assertion failure

5. These codes cause a assertion failure..

6. CAsyncSocket::Create() in thread causes assertion failure

7. MFC Callback Causes Assertion Failure

8. UpdateData(FALSE) causes assertion failure

9. Tooltips cause a assertion failure in regular dll

10. Tooltips used in dll cause assertion failure

11. CSocket in Dll causes an assertion failure

12. Multiple fonts cause assertion failure

 

 
Powered by phpBB® Forum Software