System.AccessViolationException using C#

4640
6
06-05-2012 01:25 AM
OlaRennemo
New Contributor III
Hi all,
I am developing a toolbar for ArcMap with C#, which can import features from external geodatabases.
For each feature to import, receiving featureclass is searched for existing values using objectid and cursors. (Cursors are closed using "Marshal.ReleaseComObject(cur);" after use.)
and the feature is inserted if needed.
This seems to work well as long as the receiving featureclass holds a small number of features, but when the number grows (ex. 100.000), I get a System.AccessViolationException after e few hundred inserts.
It looks as if the exception is thrown during FeatureClass.CreateFeature. But it is difficult to examine in the debugger, since it looks as if there is no specific number of inserts before the crash occurs.

Using GDB file geodatabase for storage.
ArcMap 10.0 SP3
VisualStudio 2010 SP1
Win7-64

Have a missed some limitations here? Any known bugs?

TIA
Ola
0 Kudos
6 Replies
sapnas
by
Occasional Contributor III
Can you post the code for import features? Also, Did you monitor the memory usage in taskmgr while running insert features?
0 Kudos
SumitSharma
New Contributor III
Can you provide details on how many features get added or updated before you get this error? Also are you using recycling cursor or non-recycling cursor?

Try releasing cursor after 40,000 or 50,000 iterations and see if that makes any difference.

Cheers!!

Sumit
0 Kudos
OlaRennemo
New Contributor III
Can you post the code for import features? Also, Did you monitor the memory usage in taskmgr while running insert features?


Memory usage seem to be normal, with plenty left.
It is unpractical to post the entire routine (many layers), but to give an idea of what is going on, here is the main operations:

//
// Initiate:
IFeatureclass fcSource = ..
IFeatureClass fcTarget = ...
IWorspaceEdit wsTarget = ...
List sourceIdList = ...
//
// Start transaction:
wsTarget.StartEditing(false);
wsTarget.StartEditOperation();
//
// Do import:
foreach(int sourceOid in sourceIdList)
{ 
 IFeature fSource = fcSource.GetFeature(sourceOid);
 ...
 int a = fSource.get_value(..);
 int b = fSource.get_value(...); 
 //
 // Examine values in target fc if necessary:
 if(boundsHaveChanged)
 {
         String whereRestriction = "(a>"+a.ToString() + ") and (b<"+b.ToString()+")";
         IQueryFilter qf = new QueryFilterClass();
         qf.WhereClause = whereRestriction;                          
         qf.AddField(fieldName);
         IFeatureCursor fcur = fl.FeatureClass.Search(qf, false);
         IDataStatistics dstat = new DataStatisticsClass();
         dstat.Cursor = (ICursor)fcur;
         dstat.Field = fieldName;
         IStatisticsResults res = dstat.Statistics;
         int maxFound = System.Convert.ToInt32(res.Maximum);
         ...
         Marshal.ReleaseComObject(fcur);
                 Marshal.ReleaseComObject(dstat);
 }
 
 IFeature fTargetNew = fcTarget.CreateFeature();
 IPoint p = new PointClass();
 ...
 fTargetNew.Shape = p;
 ...  
 fTargetNew.set_value(i, f.get_value(j) );
 ...
 fTargetNew.Store();
}
wsTarget.StopEditOperation();
wsTarget.StopEditing(true);
...
0 Kudos
OlaRennemo
New Contributor III
Can you provide details on how many features get added or updated before you get this error? Also are you using recycling cursor or non-recycling cursor?

Try releasing cursor after 40,000 or 50,000 iterations and see if that makes any difference.

Cheers!!

Sumit


Thanks for answering.
I have tried that in other situations, and it helped. But not here.
Only a few hundred features are inserted before the crash.
I will reexamine cursor usage. See also example code in above answer.
0 Kudos
NeilClemmons
Regular Contributor III
You should release the feature objects you're using inside the loop in each loop iteration.  The ones I see are fSource, fTargetNew and f (at least it looks like f is a feature object).  Call ReleaseComObject on these feature objects inside the loop just as soon as you're done with them just like you do for cursor objects.
0 Kudos
OlaRennemo
New Contributor III
You should release the feature objects you're using inside the loop in each loop iteration.  The ones I see are fSource, fTargetNew and f (at least it looks like f is a feature object).  Call ReleaseComObject on these feature objects inside the loop just as soon as you're done with them just like you do for cursor objects.


I have done that, but unfortunately the problem is still there.
I have a feeling that there is a fundamental error here that I have overlooked...
0 Kudos