.NET

Reply
Active Member
Akanath
Posts: 7
Registered: ‎11-15-2012
Message 1 of 3 (348 Views)
Accepted Solution

AccessViolationException after read 2 files

348 Views, 2 Replies
11-15-2012 08:50 AM

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

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 2 of 3 (327 Views)

Re: AccessViolationException after read 2 files

11-15-2012 10:30 AM 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.

Active Member
Akanath
Posts: 7
Registered: ‎11-15-2012
Message 3 of 3 (278 Views)

Re: AccessViolationException after read 2 files

11-21-2012 01:57 AM in reply to: Akanath

Of course.

2 file for 2 Database :smileyhappy:

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

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.