.NET

.NET

Reply
Valued Contributor
99 Posts
4 Kudos
Registered: ‎10-03-2011
Post 1 of 1

Document.CloseAndSave: Attempted to read or write protected memory

513 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
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!