.NET

.NET

Reply
Valued Contributor
Ertqwa
Posts: 99
Registered: ‎10-03-2011
Message 1 of 1 (503 Views)

Document.CloseAndSave: Attempted to read or write protected memory

503 Views, 0 Replies
05-01-2012 10:40 PM

Hello Forum,

 

I have code that throws the following error:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

At Autodesk.AutoCAD.ApplicationServices.Document.CloseInternal()

At Autodesk.AutoCAD.ApplicationServices.Document.CloseAndSave()

 

It only happens with some drawings. 

 

Why would "Document.CloseAndSave()" throw this error? How can I prevent this/check if the document is ok to be closed and saved?

 

using (Document docTarget = CADS.Application.DocumentManager.Open(strTargetDwgPath, false))
{
    try
    {
        if (!docTarget.IsReadOnly)
        {
            using (DocumentLock dlTarget = docTarget.LockDocument())
            {
                using (Transaction transTarget = docTarget.Database.TransactionManager.StartTransaction())
                {
                    btTarget = transTarget.GetObject(docTarget.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
                    foreach (string strDwgPath in strSourceDwgPathList)
                    {
                        ++intIndex;
                        if (btTarget.Has(System.IO.Path.GetFileNameWithoutExtension(strDwgPath)))
                        {
                            using (Database dbInsert = new Database(false, true))
                            {
                                dbInsert.ReadDwgFile(strDwgPath, System.IO.FileShare.Read, true, "");
                                oidInsert = docTarget.Database.Insert(System.IO.Path.GetFileNameWithoutExtension(strDwgPath), dbInsert, true);
                            }
                            btrInsert = transTarget.GetObject(oidInsert, OpenMode.ForWrite) as BlockTableRecord;
                            if (btrInsert.IsDynamicBlock)
                                btrInsert.UpdateAnonymousBlocks();
                            ++intFound;
                        }
                        else
                            ++intNotFound;
                    }
                    btTarget = null;
                    transTarget.Commit();
                }
            }
            // Save changes.
            docTarget.CloseAndSave(strTargetDwgPath); // <<<< ERROR.
            enuResult = DialogResult.OK;
        }
        else
        {
            docTarget.CloseAndDiscard();
            mstrMessage = "Failed: file is readonly or in use.";
            enuResult = DialogResult.Cancel;
        }
    }
    catch (System.Exception Ex)
    {
        MessageBox.Show(Ex.ToString(), "A");
        mstrMessage = "Error redefining blocks in multiple drawings: " + Ex.Message + " (" + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + ").";
        if (docTarget != null) { docTarget.CloseAndDiscard(); }
        enuResult = DialogResult.Cancel;
    }
}

 


Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.