Assertion Failure on Delete[]

Hi,
I'm using ATL VC++ 6.0 writing a button for ArcMap and this code Is giving me an error and I'm completely baffled. I have commented it out to a frustrating bare minimum of:

<code>
//get corordinate and adjacency information from a layer
IGeoFeatureLayerPtr igeoflayer;
IFeatureClassPtr featureClass;
long num_edges, num_verts;
VARIANT_BOOL Recycling;
long featureCount;
IFeatureCursorPtr Cursor;
IFeaturePtr object;
IGeometryPtr shape;
ISegmentCollectionPtr pCollection;
long pointCount=0;
ISegmentPtr point;
long fcounter=0;
long pcounter=0;
IPolylinePtr pPLine;
IPointPtr segfrompoint,segtopoint, pPoint;
IFieldsPtr pFields1(CLSID_Fields);
CComBSTR ID, doubles1, doubles2("mt");
long tPointIndex, fPointIndex, Feature_Edge_ID=-1, last_to;
long * dub_edge_ids;
long ** new_kids;
double extent[4];
double fp_x,fp_y,tp_x,tp_y;
double length;
double * recordX;
double * recordY;
double ratio, step = .5;
long * degreetally;
edge * edges;
long i=0, j=0, ecounter=0, fid=-1, k=0;
bool from=TRUE, to=TRUE, dup = false, isLoop=false, backOnTrack=TRUE;

USES_CONVERSION;

toleranceZ = 0;

toleranceX = toleranceX/10000;
toleranceY = toleranceY/10000;
toleranceZ = toleranceZ/10000;

iLayer->QueryInterface(IID_IGeoFeatureLayer, (void**) &igeoflayer); //query for interface in featurelayer object

igeoflayer->get_FeatureClass(&featureClass);

Recycling = VARIANT_TRUE;

num_edges=4*getNumbers(featureClass, num_verts);

num_verts = 2*num_verts;

logFile<<"Num edges ="<<num_edges<<" Num Verts="<<num_verts<<endl;

featureClass->Search(NULL, VARIANT_FALSE, &Cursor );
if( Cursor == NULL){
::MessageBox(0, "Cursor Error -- File is Read Only", "Start", MB_OK); return -1;}

featureClass->FeatureCount(NULL, &featureCount);

long marker=0;

recordX = new double[num_verts];
recordY = new double[num_verts];
new_kids = new long*[num_verts];
degreetally = new long[num_verts];
dub_edge_ids = new long[num_edges];
edges = new edge[num_edges];
degreetally[0]=0;


logFile<<"DEC Num edges ="<<num_edges<<" Num Verts="<<num_verts<<endl;

for(i=0; i<num_verts;i++)
{
new_kids[i] = new long[num_edges];
}

logFile<<"Exiting..."<<endl;

logFile<<"DELETE Num edges ="<<num_edges<<" Num Verts="<<num_verts<<endl;

//Chokes right here on delete[]!!!!

for( i=0; i<num_verts; i++ )
{
delete[] new_kids;
}

logFile<<"DELETE Num edges ="<<num_edges<<" Num Verts="<<num_verts<<endl;
logFile<<"deleting the rest...."<<endl;
delete[] recordX;
delete[] recordY;
delete[] degreetally;
delete[] edges;
delete[] dub_edge_ids;
//logFile<<"done processing layer"<<endl;

return num_verts;
</code>

any ideas would be greatly appreciated. I have, via the log verified that non eof the varible values changes from the time it's declared and to the time it's deleted. I'm desparate, please help.

1
2
3
4
5
6
//Chokes right here on delete[]!!!!

for( i=0; i<num_verts; i++ )
{
delete[] new_kids;
}


Yes, it would choke there. Your are trying to delete the same array more than once!

Each element of the new_kids array is a pointer which points to
an array of its own.
What you probably wanted to do is:
1
2
3
4
5
6
7
8
//First delete the individual arrays pointed to by each member of the new_kids array
for( i=0; i<num_verts; i++ )
{
    delete[] new_kids[i];
}

//now delete the new_kids array
delete [] new_kids;
Last edited on
Topic archived. No new replies allowed.