Protected memory issue while modifying shapefile data

90
2
Jump to solution
06-18-2020 07:56 AM
Sai_PhaneendraPoludasu2
New Contributor II

Hi,

There are around 50k records in a shapefile feature class. I am using following code block to modify the features.

private async Task UpdateData(FeatureLayer layer)
{
using (FeatureClass oInFC = layer.GetFeatureClass())
{
var featCursor = oInFC.Search(null, false);

EditOperation editOperation = new EditOperation();
editOperation.Callback(context =>
{
while (featCursor.MoveNext())
{
var feat = featCursor.Current;

//Modify feature attributes
feat.Store();
context.Invalidate(feat);
}
}, oInFC);

try
{
var result = await editOperation.ExecuteAsync();
if (!result)
{
throw new Exception(editOperation.ErrorMessage);
}
}
catch(Exception ex)
{
//Handle Exception
}
}
}

I see a protected memory access violation exception randomly. Any idea why this might be occurring ?

Thanks,
Sai

0 Kudos
1 Solution

Accepted Solutions
GintautasKmieliauskas
Regular Contributor

Hi Sai,

ArcGIS Pro SDK recommends use of 'using' when you work with database objects. It will manage object life cycle.

Try change your code like this:

 using (var feat = featCursor.Current)

{

                        //Modify feature attributes
                        feat.Store();
                        context.Invalidate(feat);

}

View solution in original post

0 Kudos
2 Replies
GintautasKmieliauskas
Regular Contributor

Hi Sai,

ArcGIS Pro SDK recommends use of 'using' when you work with database objects. It will manage object life cycle.

Try change your code like this:

 using (var feat = featCursor.Current)

{

                        //Modify feature attributes
                        feat.Store();
                        context.Invalidate(feat);

}

View solution in original post

0 Kudos
Sai_PhaneendraPoludasu2
New Contributor II

Thanks for spotting the issue @Gintautas_Kmieliauskas. I missed that.

I changed the code to a using block and it worked.

Cheers!

0 Kudos