.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AccessViolationException after read 2 files

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Akanath
1329 Views, 2 Replies

AccessViolationException after read 2 files

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;
}

 

2 REPLIES 2
Message 2 of 3
DiningPhilosopher
in reply to: Akanath


@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.

Message 3 of 3
Akanath
in reply to: Akanath

Of course.

2 file for 2 Database 🙂

Thank you for your reply, and sorry for this question of new user of Autocad.NET

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost