.NET

Reply
Valued Contributor
mrevelli
Posts: 84
Registered: ‎10-21-2003
Message 1 of 5 (238 Views)

BlockTableRecord Error " write protected memory"

238 Views, 4 Replies
01-24-2006 08:58 AM
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
*Tony Tanzillo
Message 2 of 5 (238 Views)

Re: BlockTableRecord Error " write protected memory"

01-24-2006 04:16 PM in reply to: mrevelli
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
Valued Contributor
mrevelli
Posts: 84
Registered: ‎10-21-2003
Message 3 of 5 (238 Views)

Re: BlockTableRecord Error " write protected memory"

01-25-2006 06:21 AM in reply to: mrevelli
Hi Tony,
I have remove CommandFlags.Session :
[CommandMethod("ad_link")]
bat the error was the same.
How can I do ?
Max
Distinguished Contributor
cgay
Posts: 125
Registered: ‎08-02-2005
Message 4 of 5 (238 Views)

Re: BlockTableRecord Error " write protected memory"

01-25-2006 02:01 PM in reply to: mrevelli
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
*Tony Tanzillo
Message 5 of 5 (238 Views)

Re: BlockTableRecord Error " write protected memory"

01-25-2006 09:26 PM in reply to: mrevelli
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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

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 to get help installing your software.

Ask the Community