[fdo-users] RE: .net Wrapper quirks
Clayton Hotson
clayton.hotson at autodesk.com
Sat Jul 24 06:49:59 EDT 2010
Bohne,
Try avoiding compound statements like the one you use to clear and add to the insert command's property values - insert.PropertyValues.Clear() and insert.PropertyValues.Add(). Instead try caching a variable outside the loop which represents the return from PropertyValues - and Dispose() it after the loop exits (the way it is written below, a PropertyValueCollection reference is actually created *twice* for each item in the reader, and never disposed, since PropertyValueCollection is also disposable).
I have seen this behavior you report many, many times, and I believe it is related to the generational nature of the garbage collector in .NET not being able to keep up with allocations made during statements like 'insert.PropertyValues.xxx()', especially in loops, and this is why the exception occurs somewhat randomly but only after a certain amount of data is processed.
Basically, there should be an explicit Dispose() called for every, single IDisposable object you access in FDO - and in loops with large amounts of data this must be taken to the absolute extreme. The 'using' is useful (as you use) and it can lead to perhaps difficult-to-read code, but you might also consider try-finally blocks for disposal of multiple objects.
As Jackie says, it is not necessary to call Dispose() on objects you wrap in the using statement, as this is done automatically when it exits scope.
Anyway, I hope this helps!
Clayton
On SatJul 24, 2010, at Sat Jul 24, 6:02 PM, Bohne wrote:
Hi Jacky,
thanks for your reply.
I modified my code but still no luck. Even if I'm adding only one integer
value to my Insertcommand (without reading from source) i'm getting this
Exception ( less than before, but still there) ??
private void writeValues(FDO.IFDOSource source, FDO.IFDOSource target,
Graphics graphics)
{
//Open Source Connection, if necessary
if (source.Connection.ConnectionState !=
OSGeo.FDO.Connections.ConnectionState.ConnectionState_Open)
source.Connection.Open();
//Open Target Connection, if necessary
if (target.Connection.ConnectionState !=
OSGeo.FDO.Connections.ConnectionState.ConnectionState_Open)
target.Connection.Open();
//Creating Select Command
ISelect select =
source.Connection.CreateCommand(OSGeo.FDO.Commands.CommandType.CommandType_Select)
as ISelect;
//setting featureClassName
select.SetFeatureClassName(source.getFeatureClassName());
//Filter source
if (graphics.Filter.Length > 0)
select.Filter =
OSGeo.FDO.Filter.Filter.Parse(graphics.Filter);
//creating InsertCommand
using (IInsert insert =
target.Connection.CreateCommand(OSGeo.FDO.Commands.CommandType.CommandType_Insert)
as IInsert)
{
//setting featureClassName
insert.SetFeatureClassName(target.getFeatureClassName());
//Counter
int counter = 0;
//create Reader
using (IFeatureReader sourceReader = select.Execute())
{
while (sourceReader.ReadNext())
{
//Clear old insertcommand
insert.PropertyValues.Clear();
//Adding PropertyValues to InsertCommand
bool writeRecord = true;// fillRow( insert, source,
sourceReader, target.MyFeatureClass, graphics, counter);
insert.PropertyValues.Add(new PropertyValue("MOVEID",
new Int32Value(counter)));
//
if (writeRecord)
{
//Executing InsertCommand
using (IFeatureReader insertReader =
insert.Execute())
{
insertReader.Close();
}
}
counter++;
}
}
}
source.Connection.Close();
target.Connection.Close();
}
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/net-Wrapper-quirks-tp2049912p5332467.html
Sent from the FDO Users mailing list archive at Nabble.com<http://Nabble.com>.
_______________________________________________
fdo-users mailing list
fdo-users at lists.osgeo.org<mailto:fdo-users at lists.osgeo.org>
http://lists.osgeo.org/mailman/listinfo/fdo-users
More information about the fdo-users
mailing list