.NET

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

Document.CloseAndSave: Attempted to read or write protected memory

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

 


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