.NET

Reply
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 1 of 7 (438 Views)
Accepted Solution

Garbage Collection Issues

438 Views, 6 Replies
06-06-2012 01:32 PM

Hey everyone,

 

So here goes my second post of the day. I've been working on a function that i just recently posted in a previous thread and am having a strange issue. I'm new to automatic garbage collection in .NET and was hoping someone could point mein the right direction. Whenever i run the below function, once it has finished creating my array, as soon as i try to run another one of my commands, i get this error:

 

A callback was made on a garbage collected delegate of type 'Acmgd!Autodesk.AutoCAD.Runtime.CommandClass+CommandThunk+CommandInvokeHandler::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.

 

Why is this, what am i doing wrong here? Any advice or pointers would be awesome.

Cheers

Vince

 

// Our list of Attributes
                    BlockReference blockRef = bref;

                    AttributeDefinition attDef1 = new AttributeDefinition(new Point3d(0d, 0d, 0d), type, "Type", "", db.Textstyle);
                    attDef1.Invisible = true;
                    AttributeDefinition attDef2 = new AttributeDefinition(new Point3d(0d, 0d, 0d), device, "Device", "", db.Textstyle);
                    attDef2.Invisible = true;
                    AttributeDefinition attDef3 = new AttributeDefinition(new Point3d(0d, 0d, 0d), kfactor, "Test", "", db.Textstyle);
                    attDef3.Invisible = true;
                    AttributeDefinition attDef4 = new AttributeDefinition(new Point3d(0d, 0d, 0d), size, "Size", "", db.Textstyle);
                    attDef4.Invisible = true;
                    AttributeDefinition attDef5 = new AttributeDefinition(new Point3d(0d, 0d, 0d), area1, "Area", "", db.Textstyle);
                    attDef5.Invisible = true;
                    AttributeDefinition attDef6 = new AttributeDefinition(new Point3d(0d, 0d, 0d), maxasp, "Test Area", "", db.Textstyle);
                    attDef6.Invisible = true;
                    AttributeDefinition attDef7 = new AttributeDefinition(new Point3d(0d, 0d, 0d), maxdsp, "Test Distance", "", db.Textstyle);
                    attDef7.Invisible = true;
                    AttributeDefinition attDef8 = new AttributeDefinition(new Point3d(0d, 0d, 0d), density, "Density", "", db.Textstyle);
                    attDef8.Invisible = true;
                    AttributeDefinition attDef9 = new AttributeDefinition(new Point3d(0d, 0d, 0d), weight, "Weight", "", db.Textstyle);
                    attDef9.Invisible = true;
                    AttributeDefinition attDef10 = new AttributeDefinition(new Point3d(0d, 0d, 0d), temp, "Temperature", "", db.Textstyle);
                    attDef10.Invisible = true;
                    AttributeDefinition attDef11 = new AttributeDefinition(new Point3d(0d, 0d, 0d), orfice, "Test", "", db.Textstyle);
                    attDef11.Invisible = true;
                    AttributeDefinition attDef12 = new AttributeDefinition(new Point3d(0d, 0d, 0d), price, "Price", "", db.Textstyle);
                    attDef12.Invisible = true;
                    AttributeDefinition attDef13 = new AttributeDefinition(new Point3d(0d, 0d, 0d), style, "Test", "", db.Textstyle);
                    attDef13.Invisible = true;
                    AttributeDefinition attDef14 = new AttributeDefinition(new Point3d(0d, 0d, 0d), finish, "Finish", "", db.Textstyle);
                    attDef14.Invisible = true;
                    AttributeDefinition attDef15 = new AttributeDefinition(new Point3d(0d, 0d, 0d), productinfo, "Web Link", "", db.Textstyle);
                    attDef15.Invisible = true;

                    
                    // Add the attribute to to the BlockTableRecord of the blockdefinition of the selected block
                    ObjectId btrId = blockRef.BlockTableRecord;
                    BlockTableRecord btrBlock = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForWrite);                                     
                    
                    btrBlock.AppendEntity(attDef1);
                    tr.AddNewlyCreatedDBObject(attDef1, true);
                    
                    btrBlock.AppendEntity(attDef2);
                    tr.AddNewlyCreatedDBObject(attDef2, true);

                    btrBlock.AppendEntity(attDef3);
                    tr.AddNewlyCreatedDBObject(attDef3, true);
                    
                    btrBlock.AppendEntity(attDef4);
                    tr.AddNewlyCreatedDBObject(attDef4, true);
                    
                    btrBlock.AppendEntity(attDef5);
                    tr.AddNewlyCreatedDBObject(attDef5, true);

                    btrBlock.AppendEntity(attDef6);
                    tr.AddNewlyCreatedDBObject(attDef6, true);
                    
                    btrBlock.AppendEntity(attDef7);
                    tr.AddNewlyCreatedDBObject(attDef7, true);
                    
                    btrBlock.AppendEntity(attDef8);
                    tr.AddNewlyCreatedDBObject(attDef8, true);

                    btrBlock.AppendEntity(attDef9);
                    tr.AddNewlyCreatedDBObject(attDef9, true);
                    
                    btrBlock.AppendEntity(attDef10);
                    tr.AddNewlyCreatedDBObject(attDef10, true);
                    
                    btrBlock.AppendEntity(attDef11);
                    tr.AddNewlyCreatedDBObject(attDef11, true);
                    
                    btrBlock.AppendEntity(attDef12);
                    tr.AddNewlyCreatedDBObject(attDef12, true);

                    btrBlock.AppendEntity(attDef13);
                    tr.AddNewlyCreatedDBObject(attDef13, true);

                    btrBlock.AppendEntity(attDef14);
                    tr.AddNewlyCreatedDBObject(attDef14, true);

                    btrBlock.AppendEntity(attDef15);
                    tr.AddNewlyCreatedDBObject(attDef15, true);

                    // Add the AttributeReference to the BlockReference                      
                
                    AttributeReference attRef1 = new AttributeReference();
                    attRef1.SetAttributeFromBlock(attDef1, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef1);
                    attRef1.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef1, true);

                    AttributeReference attRef2 = new AttributeReference();
                    attRef2.SetAttributeFromBlock(attDef2, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef2);
                    attRef2.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef2, true);

                    AttributeReference attRef3 = new AttributeReference();
                    attRef3.SetAttributeFromBlock(attDef3, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef3);
                    attRef3.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef3, true);

                    AttributeReference attRef4 = new AttributeReference();
                    attRef4.SetAttributeFromBlock(attDef4, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef4);
                    attRef4.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef4, true);

                    AttributeReference attRef5 = new AttributeReference();
                    attRef5.SetAttributeFromBlock(attDef5, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef5);
                    attRef5.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef5, true);

                    AttributeReference attRef6 = new AttributeReference();
                    attRef6.SetAttributeFromBlock(attDef6, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef6);
                    attRef6.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef6, true);

                    AttributeReference attRef7 = new AttributeReference();
                    attRef7.SetAttributeFromBlock(attDef7, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef7);
                    attRef7.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef7, true);

                    AttributeReference attRef8 = new AttributeReference();
                    attRef8.SetAttributeFromBlock(attDef8, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef8);
                    attRef8.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef8, true);

                    AttributeReference attRef9 = new AttributeReference();
                    attRef9.SetAttributeFromBlock(attDef9, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef9);
                    attRef9.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef9, true);

                    AttributeReference attRef10 = new AttributeReference();
                    attRef10.SetAttributeFromBlock(attDef10, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef10);
                    attRef10.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef10, true);

                    AttributeReference attRef11 = new AttributeReference();
                    attRef11.SetAttributeFromBlock(attDef11, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef11);
                    attRef11.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef11, true);

                    AttributeReference attRef12 = new AttributeReference();
                    attRef12.SetAttributeFromBlock(attDef12, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef12);
                    attRef12.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef12, true);

                    AttributeReference attRef13 = new AttributeReference();
                    attRef13.SetAttributeFromBlock(attDef13, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef13);
                    attRef13.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef13, true);

                    AttributeReference attRef14 = new AttributeReference();
                    attRef14.SetAttributeFromBlock(attDef14, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef14);
                    attRef14.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef14, true);
                   
                    AttributeReference attRef15 = new AttributeReference();
                    attRef15.SetAttributeFromBlock(attDef15, blockRef.BlockTransform);
                    blockRef.AttributeCollection.AppendAttribute(attRef15);
                    attRef15.AdjustAlignment(db);
                    tr.AddNewlyCreatedDBObject(attRef15, true);
                                           
                    
                    // These two are disabled as they generate an old block reference made up of circles and lines
                    //bref.ExplodeToOwnerSpace();
                    // bref.Erase();

                    //Create a rectangular array with 5 rows and 5 columns
                    double nRows = prilines;
                    double nColumns = seclines;
                    
                    //Set the row and column offsets along with the base array angle
                    double dRowOffset = msprinkdist;
                    double dColumnOffset = mlinedist;
                    double dArrayAng = 0;

                    //Get the angle from x for the current UCS
                    Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
                    CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
                    Vector2d acVec2dAng = new Vector2d(curUCS.Xaxis.X, curUCS.Xaxis.Y);

                    //If the UCS is rotated, adjust the array angle accordingly
                    dArrayAng = dArrayAng + acVec2dAng.Angle;

                    //Use the upper-left corner of the objects extents for the array base point
                    Extents3d acExts = bref.Bounds.GetValueOrDefault();
                    Point2d acPt2dArrayBase = new Point2d(acExts.MinPoint.X, acExts.MaxPoint.Y);

                    //Track the objects created for each column
                    DBObjectCollection acDBObjCollCols = new DBObjectCollection();
                    acDBObjCollCols.Add(bref);

                    //Create the number of objects in the first column
                    int nColumnsCount = 1;
                    while (nColumns > nColumnsCount)
                    {
                        BlockReference acEntClone = bref.Clone() as BlockReference;
                        acDBObjCollCols.Add(acEntClone);
                        
                        //Calculate the new point for the copied object (move)
                        Point2d acPt2dTo = PolarPoints(acPt2dArrayBase, dArrayAng, dColumnOffset * nColumnsCount);
                        Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
                        Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
                        acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));

                        btr.AppendEntity(acEntClone);
                        acTrans.AddNewlyCreatedDBObject(acEntClone, true);

                        acEntClone.ExplodeToOwnerSpace();
                        nColumnsCount = nColumnsCount + 1;
                    }

                    //Set a value in radians for 90 degress
                    double dAng = Math.PI / 2;

                    //Track the objects created for each row and column
                    DBObjectCollection acDBObjCollLvls = new DBObjectCollection();

                    foreach (DBObject acObj in acDBObjCollCols)
                    {
                        acDBObjCollLvls.Add(acObj);
                    }

                    // Create the number of objects for each row
                    foreach (Entity acEnt in acDBObjCollCols)
                    {
                        int nRowsCount = 1;                       

                        while (nRows > nRowsCount)
                        {
                            BlockReference acEntClone = acEnt.Clone() as BlockReference;
                            acDBObjCollLvls.Add(acEntClone);

                            //Calculate the new point for the copied object (move)
                            Point2d acPt2dTo = PolarPoints(acPt2dArrayBase, dArrayAng + dAng, dRowOffset * nRowsCount);
                            Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
                            Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
                            acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));

                            btr.AppendEntity(acEntClone);
                            acTrans.AddNewlyCreatedDBObject(acEntClone, true);                                                                         
                            
                            nRowsCount = nRowsCount + 1;                            

                        }
                    }

                    tr.Commit();
                    ed.Regen();
                }
            }
        }

    }
}
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 2 of 7 (389 Views)

Re: Garbage Collection Issues

06-14-2012 01:04 PM in reply to: vince1327

So after countless hours of research and testing, it turned out all that I had to do was enable unmanaged code debugging in Visual Studio 2010...problem solved :smileyhappy: I'm not quite sure how i feel about the solution or the error, but now it runs so I shall smile and carry on.

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 3 of 7 (377 Views)

Re: Garbage Collection Issues

06-14-2012 03:30 PM in reply to: vince1327

I would be interested in why also.

 

Just a note you can shorten your code a bit by using loops.

 

List<string> myList = new List<string>(new string[] {
	"Type",
	"Device",
	"kfactor",
	"Size",
	"area1",
	"Maxasp",
	"Maxdsp",
	"Density",
	"Weight",
	"Temp",
	"Orfice",
	"Price",
	"Style",
	"finish",
	"Productinfo"
});

List<string> myType = new List<string>(new string[] {
	Type,
	device,
	kfactor,
	size,
	area1,
	maxasp,
	maxdsp,
	density,
	weight,
	temp,
	orfice,
	price,
	style,
	finish,
	productinfo
});

List<AttributeDefinition> myAttDef = new List<AttributeDefinition>();

for (int i = 0; i <= myList.Count - 1; i++) {
	AttributeDefinition AttDef = new AttributeDefinition(new Point3d(0.0, 0.0, 0.0), myType[i], myList[i], "", db.Textstyle);
	AttDef.Invisible = true;
	myAttDef.Add(AttDef);

}
// use the list array to refer to the attribute definitions. 

 

---------------------------



(defun botsbuildbots() (botsbuildbots))
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 4 of 7 (345 Views)

Re: Garbage Collection Issues

06-18-2012 07:31 AM in reply to: vince1327

Thanks for the help with the loops, i've been meaning to get around to that!!! Where are you getting "List" from? Sorry if it seems basic

 

Cheers

Vince

Valued Mentor
jeff
Posts: 328
Registered: ‎05-12-2009
Message 5 of 7 (340 Views)

Re: Garbage Collection Issues

06-18-2012 08:41 AM in reply to: vince1327

using System.Collections.Generic;

You can also find your answers @ TheSwamp
Distinguished Contributor
eljobe
Posts: 704
Registered: ‎01-24-2003
Message 6 of 7 (339 Views)

Re: Garbage Collection Issues

06-18-2012 08:42 AM in reply to: vince1327

From System.Collections.Generic. When you create a new project, a reference and using statement get added automatically. Also, you could look up an answer using the Object Browser (from the View menu).

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 7 of 7 (337 Views)

Re: Garbage Collection Issues

06-18-2012 08:44 AM in reply to: eljobe

I just figured it out as you responded haha, thanks a million, i'll get to it then.

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 or visit the Installation and Licensing Forum to get help installing your software.