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

BlockTableRecord Error " write protected memory"

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
458 Views, 4 Replies

BlockTableRecord Error " write protected memory"

Hi all,
I have this code that crash vith visual studio 2005 & Autocad 2006 :
public class DBMain
{
[CommandMethod("ad_link",CommandFlags.Session )]
public static void OpenDB()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

//...
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
BlockTable bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
//BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt["*MODEL_SPACE"], OpenMode.ForRead );
try
{
int count = 0;
foreach (ObjectId id in btr)
{
count += 1;
}
ed.WriteMessage(count.ToString());
}
catch (SystemException ex)
{
}
}
catch(Autodesk.AutoCAD.Runtime.Exception ex )
{
}
finally
{
trans.Dispose();
}
}
}

in line :
foreach (ObjectId id in btr)
{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

Anyone can help me ?
Thanks in advance
Max
4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Anonymous

Your command is registered with CommandFlags.Session,
(meaning it runs in the application context). Hence, you
must lock the Document whose Database you're accessing
before you try to do anything.

Generally, you only need to run your command in the
application context if there is some interaction with
multiple documents (such as closing or opening one,
or changing the active document).

If your command simply operates on the active document,
you don't need CommandFlags.Session.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:5067115@discussion.autodesk.com...
Hi all,
I have this code that crash vith visual studio 2005 & Autocad 2006 :
public class DBMain
{
[CommandMethod("ad_link",CommandFlags.Session )]
public static void OpenDB()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

//...
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
BlockTable bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
//BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt["*MODEL_SPACE"], OpenMode.ForRead );
try
{
int count = 0;
foreach (ObjectId id in btr)
{
count += 1;
}
ed.WriteMessage(count.ToString());
}
catch (SystemException ex)
{
}
}
catch(Autodesk.AutoCAD.Runtime.Exception ex )
{
}
finally
{
trans.Dispose();
}
}
}

in line :
foreach (ObjectId id in btr)
{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

Anyone can help me ?
Thanks in advance
Max
Message 3 of 5
Anonymous
in reply to: Anonymous

Hi Tony,
I have remove CommandFlags.Session :
[CommandMethod("ad_link")]
bat the error was the same.
How can I do ?
Max
Message 4 of 5
cgay
in reply to: Anonymous

Max

Try this

[code]
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace AcadTest
{
public class DBMain
{
[CommandMethod("ad_link")]
public static void OpenDB()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

//...
Database db = HostApplicationServices.WorkingDatabase;
// if we use the using construct, we don't have to worry about calling trans.dispose
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
BlockTable bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

try
{
int count = 0;
foreach (ObjectId id in btr)
{
if (id.IsValid)
{
count += 1;
}
}
ed.WriteMessage(count.ToString());
trans.Commit;
}
catch (SystemException ex)
{
ed.WriteMessage(ex.Message.ToString());
trans.Abort;
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(ex.Message.ToString());
trans.Abort;
}
}

}
}
}
[/code]

Good Luck,
C
Message 5 of 5
Anonymous
in reply to: Anonymous

What is "SystemException" ? Is there a missing dot there?

Also, I'm not sure I see the rationale behind catching
exceptions to abort the transaction.

While that was necessary in AutoCAD 2005 because the
transaction was not implicitly aborted if Commit() was not
called, I don't see any need for this in 2006-specific code.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:5068780@discussion.autodesk.com...
Max

Try this

[code]
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace AcadTest
{
public class DBMain
{
[CommandMethod("ad_link")]
public static void OpenDB()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

//...
Database db = HostApplicationServices.WorkingDatabase;
// if we use the using construct, we don't have to worry about calling trans.dispose
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
BlockTable bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

try
{
int count = 0;
foreach (ObjectId id in btr)
{
if (id.IsValid)
{
count += 1;
}
}
ed.WriteMessage(count.ToString());
trans.Commit;
}
catch (SystemException ex)
{
ed.WriteMessage(ex.Message.ToString());
trans.Abort;
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(ex.Message.ToString());
trans.Abort;
}
}

}
}
}
[/code]

Good Luck,
C

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