.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
AccessViol ationExcep tion after read 2 files
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hello,
I use RealDWG2013 and have some problems.
I made a migration of files and batch in C# program.
But, sometime, I have problems of AccessViolationException (Attempted to Read or write protected memory).
My program read 2 files with RealDWG for changing the cartridge recover in the first access. It's put in the 2 files.
Do you have any idea ?
Database db = new Database(false, true);
Autocad.WorkingDatabase = db;
//Read the first DWG
db.ReadDwgFile(CartridgeFullName, FileShare.ReadWrite, false, string.Empty);
ObjectIdCollection objectIdCollectionCartridge = GetCartridge(db);
//Read the target file
db = new Database(false, true);
Autocad.WorkingDatabase = db;
db.ReadDwgFile(tempFullName, FileShare.ReadWrite, false, string.Empty);
try
{
Database dbb = objectIdCollectionCartridge[0].Database; // I TRY TO ACCESS TO DATABASE OF THE CARTRIDGE. IT'S HERE THAT THE ERROR IS CATCHED
dbb.Dispose();
}
catch(){}
db.SaveAs(tempFullName, DwgVersion.AC1800);
db.CloseInput(true);
db.Dispose();
......
private ObjectIdCollection GetCartridge(Database db)
{
ObjectId msId, psId;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
msId = bt[BlockTableRecord.ModelSpace];
psId = bt[BlockTableRecord.PaperSpace];
tr.Commit();
}
return GetCartridge(psId, db);
}
private ObjectIdCollection GetCartridge(ObjectId btrId, Database db)
{
ObjectIdCollection ObjCollection = null;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
foreach (ObjectId entId in btr)
{
Entity ent = tr.GetObject(entId, OpenMode.ForRead) as Entity;
if (ent != null)
{
BlockReference br = ent as BlockReference;
if (br != null)
{
BlockTableRecord bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord , OpenMode.ForRead);
if (bd.Name.ToUpper() == BLOCK_NAME_CARTOUCHE || bd.Name.ToUpper() == BLOCK_NAME_CARTOUCHE_A0)
{
try
{
ObjCollection = new ObjectIdCollection();
foreach (ObjectId obj in bd)
{
Entity ento = tr.GetObject(obj, OpenMode.ForRead) as Entity;
Line line = ento as Line;
if (line != null)
{
ObjCollection.Add(line.ObjectId);
}
AttributeDefinition atr = ento as AttributeDefinition;
if (atr != null)
{
ObjCollection.Add(atr.ObjectId);
}
DBText txt = ento as DBText;
if (txt != null)
{
ObjCollection.Add(txt.ObjectId);
}
Polyline pl = ento as Polyline;
if (pl != null)
{
ObjCollection.Add(pl.ObjectId);
}
Hatch ha = ento as Hatch;
if (ha != null)
{
ObjCollection.Add(ha.ObjectId);
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{ }
catch (System.Exception ex)
{ }
}
}
}
}
tr.Commit();
}
return ObjCollection;
}
Solved! Go to Solution.
Re: AccessViol ationExcep tion after read 2 files
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Akanath wrote:Hello,
I use RealDWG2013 and have some problems.
I made a migration of files and batch in C# program.
But, sometime, I have problems of AccessViolationException (Attempted to Read or write protected memory).
My program read 2 files with RealDWG for changing the cartridge recover in the first access. It's put in the 2 files.
Do you have any idea ?
Database db = new Database(false, true); Autocad.WorkingDatabase = db; //Read the first DWG db.ReadDwgFile(CartridgeFullName, FileShare.ReadWrite, false, string.Empty); ObjectIdCollection objectIdCollectionCartridge = GetCartridge(db); //Read the target file db = new Database(false, true); Autocad.WorkingDatabase = db; db.ReadDwgFile(tempFullName, FileShare.ReadWrite, false, string.Empty); try { Database dbb = objectIdCollectionCartridge[0].Database; // I TRY TO ACCESS TO DATABASE OF THE CARTRIDGE. IT'S HERE THAT THE ERROR IS CATCHED dbb.Dispose(); } catch(){}
db.SaveAs(tempFullName, DwgVersion.AC1800);
db.CloseInput(true);
db.Dispose();
It looks like you are assigning two databases to the same 'db' variable, but you don't call Dispose() on the first Database assigned to that variable before reassigning it to the second Database. You have to keep a reference to a Database in order for it to not be garbage-collected, and you must call Dispose() once you're done using it.
Re: AccessViol ationExcep tion after read 2 files
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Of course.
2 file for 2 Database ![]()
Thank you for your reply, and sorry for this question of new user of Autocad.NET

