I hope someone of your can figure this one out.
There is an error "Corrupted memory" when on several machines executing some code. I know the error is in the wblockcloneobjects from drawing A to drawing B.
The strange thing about this error is:
We have 6 computers with Civil and Autocad.
Some users have no errors at all.
Some people only errors when executing it in Civil, som only when executing it in Autocad, some have it in both.
This is almost a copy of the VB.NET sample from autodesk.
This is the code where it crashes:
Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.Runtime Public Class Class1 <CommandMethod("TEST", CommandFlags.Session)> Public Sub CopyObjecten() Dim oAppCivilDWT As String = "C:\Templates\Civil.dwt" Dim oAppAutocadDWT As String = "C:\Templates\Autocad.dwt" Dim oAppMechanicalDWT As String = "C:\Templates\Mechanical.dwt" RunMyCode(oAppAutocadDWT, oAppCivilDWT, oAppMechanicalDWT) End Sub Public Sub RunMyCode(ByVal oAppAutocadDWT, ByVal oAppCivilDWT, ByVal oAppMechanicalDWT) Dim template As String = oAppAutocadDWT Dim oCadVer As String = Strings.Left(Strings.Right(SystemObjects.DynamicLinker.ProductKey, 7), 3) If oCadVer = "001" Then 'Autocad template = oAppAutocadDWT ElseIf oCadVer = "000" Then 'Civil template = oAppCivilDWT ElseIf oCadVer = "005" Then 'Mechanical template = oAppMechanicalDWT End If Dim oObjColl As ObjectIdCollection = New ObjectIdCollection() Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Using lockdoc As DocumentLock = doc.LockDocument() Using tr As Transaction = db.TransactionManager.StartTransaction() Dim blktbl As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) Dim blktblrec As BlockTableRecord = tr.GetObject(blktbl(BlockTableRecord.ModelSpace), OpenMode.ForRead) oObjColl = New ObjectIdCollection() For Each oObjID As ObjectId In blktblrec oObjColl.Add(oObjID) Next End Using End Using Dim docmgr As DocumentCollection = Application.DocumentManager Dim nieuw As Document = docmgr.Add(template) 'template Dim nieuwdb As Database = nieuw.Database Using lockdocnieuw As DocumentLock = nieuw.LockDocument() Using tr = nieuwdb.TransactionManager.StartTransaction() Dim acBlkTblNewDoc As BlockTable = tr.GetObject(nieuwdb.BlockTableId, OpenMode.ForRead) Dim acBlkTblRecNewDoc As BlockTableRecord = tr.GetObject(acBlkTblNewDoc(BlockTableRecord.ModelSpace), OpenMode.ForRead) Dim acIdMap As IdMapping = New IdMapping() db.WblockCloneObjects(oObjColl, acBlkTblRecNewDoc.ObjectId, acIdMap, DuplicateRecordCloning.Ignore, False) tr.Commit() End Using End Using docmgr.MdiActiveDocument = nieuw End Sub End Class
All the modelpace objects needs to be copied to a template. (the full code does more stuff but this is the critical part)
Still crashes on some machines, I've added a Try Catch to it.
the ex.message
"Unhandled exception has occured in a component in your application. If you click Continue, the application will ignore this error and attempt to continue.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I'm confused its so basic and doesn't work.
Hi there,
I had this error last week on some of my blocks that I was using when I used code to import them. The way I got round it, and this isn't an reason why it happens, was to re-create the offending blocks. This worked for me, but it was completly random and very fustraiting.
I've had similar issues especially if the blocks were created back in 2004. What did you use to "recreate" the block? Can you post some code. Thank You.
Hi Articad,
the blocks I recreated were for example: A block with attributes. I exploded it, copied the objectss into a new drawing, and re-did the block. Then when my code accessed it, it seemed to work. Oh & I gave it a new name. It wasn't actually the cope itself that was the problem. For me it just seemed to be the old block. The code to insert the block though was as follows:
Dim blkname As String = "Name"
Dim fname As String = "C:\Program Files\Block Library\" & blkname & ".dwg"
Dim db As Database = New Database(False, False)
db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)
Dim idBTR As ObjectId = myDWG.Database.Insert(fname, db, False)
Dim bref as BlockReference= New BlockReference(InsPt, idBTR)
acBlkTblRec.AppendEntity(bref)
acTrans.AddNewlyCreatedDBObject(bref, True)
Like I said though it work on say 10 out of 12 blocks in the folder, but when I changed to block into a new one with a new name, it worked. Good luck.
Quigs
@Anonymous wrote:Hi Articad,
the blocks I recreated were for example: A block with attributes. I exploded it, copied the objectss into a new drawing, and re-did the block. Then when my code accessed it, it seemed to work. Oh & I gave it a new name. It wasn't actually the cope itself that was the problem. For me it just seemed to be the old block. The code to insert the block though was as follows:
Dim blkname As String = "Name"
Dim fname As String = "C:\Program Files\Block Library\" & blkname & ".dwg"
Dim db As Database = New Database(False, False)
db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)
Dim idBTR As ObjectId = myDWG.Database.Insert(fname, db, False)
Dim bref as BlockReference= New BlockReference(InsPt, idBTR)
acBlkTblRec.AppendEntity(bref)
acTrans.AddNewlyCreatedDBObject(bref, True)
Like I said though it work on say 10 out of 12 blocks in the folder, but when I changed to block into a new one with a new name, it worked. Good luck.
Quigs
Quigs give this a try with changes in red
Dim db As Database = New Database(False, True)
db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)
Dim idBTR As ObjectId = myDWG.Database.Insert(fname, db, True)
RPeter,
Take another look at 2nd post by Jeff_M
http://www.theswamp.org/index.php?topic=37401.msg426363#msg426363
If I understand it (post http://www.theswamp.org/index.php?topic=37401.msg426363#msg426363) I need to loop tru all the BlockTableRecords of drawing A, add the names as block, add its ID and saves it to the transaction before if collect all the objects?
(The drawing A is an Export of ArcGis)
Not to worry about the link so much it had a little more involved things going on.
The main thing Jeff_M discovered with Civil3D is when Database.WblockCloneObjects() is called the document of the database you are adding the objects to needs to be current.
So if you copying blocks or entites in model space from DrawingA to DrawingB when WblockCloneObjects() is called DrawingB needs to be the open, current, drawing.
So in short from your code this
docmgr.MdiActiveDocument = nieuw
needs to come before
db.WblockCloneObjects(oObjColl, acBlkTblRecNewDoc.ObjectId, acIdMap, DuplicateRecordCloning.Ignore, False)
I already added it to my code, no success.
Still some users crashes, some in Autocad Some in Civil some in both of them.
Maybe this is a stupid thought but here we go:
I build the dll in VS2010 Express, Acad 2010,2011,2012 are compiled in VS2008, would it be a difference if I would build them in VS2008? (target framework is set to 3.5 wich is available on all machines => WIN7)
Are you using same bulid for different years & verticals?
Are you using same drawing to test?
Which ones are failing and is there anything in common like year, 32bit vs 64bit?
So
Acad 2010 pass
Acad 2011 Fail
Civil 2010 pass
etc...............
All the computers are identical Workstations, WIN 7 64 bit, 2011 products (for now)
Only difference, I have VS2010 express installed (comes with .NET FW 4.0)
We all use the same CAD profile, same test drawing,...
I have Admin privileges when I log on, but this is excluded because I did the test for giving an other Admin privileges as well but had no solution.
The error is not always there... some users can execute it without a problem. Later that day (after som other work has been done) it doesn't work annymore. (Neither when de Product is restarted)
Some users only have troubles in Autocad others in Civil some in both.
I already tried to delete there acad, civil and mechanical profile so the "First Run installer" would run again to set all the registery values. This wasn't a solution.
Personel I have this error somethimes, this occures only when I opened a lot of drawings during the day and try to run the routine afterwards, restart of the cad application helps for me.
I'm getting realy **confused** same application on identical hard and software (VS excluded).
Are you targeting 3.5 for 2011?
Is the exact code posted causing the errors or if you just use the code posted with the MidiActiveDocument issue fixed and no other code do errors occur?
Its the exact code that gives errors wile executing.
The other code hase no errors, I pulled out this part of code and putted it in another app just to test this small code. Including the MdiActiveDocument fix.
I also tested to build it with VS2008 => same problem.
In VS2010 the target FW = 3.5
I found the problem but now how the *** do I solve this one
When you start up autocad and layermanager is open => code will crash (only first start)
When you start up autocad and layermanager is not open => code will run just fine
When layer manager is opened during a drawing and closed or still open => code will crash
Can't find what you're looking for? Ask the community or share your knowledge.