Author Topic: Question about disposing.  (Read 14452 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Question about disposing.
« Reply #15 on: October 26, 2009, 07:24:48 PM »
I thought I saw that in an example somewhere. < shrug >

Since you did not create the database, don't dispose it.

Here is how I been calling my methods:

That's fine, you just didn't show us the caller of your function.

have a look at this code and see if it works for you

Code: [Select]
public void RemoveGroup(string Strline)
  {
   AcAp.Document activeDocument = AcAp.Application.DocumentManager.MdiActiveDocument;
   AcEd.Editor editor = activeDocument.Editor;
   Database database = HostApplicationServices.WorkingDatabase;

   using (DocumentLock doclck = activeDocument.LockDocument())
   {
    using (Transaction trans = database.TransactionManager.StartTransaction())
    {
     DBDictionary GrpDic =
      trans.GetObject(database.GroupDictionaryId, OpenMode.ForRead) as DBDictionary;

     if (GrpDic.Contains(Strline))
     {
      ObjectId Did = ObjectId.Null;
      Did = GrpDic.GetAt(Strline);

      if (!Did.IsNull)
      {
       Group EntGrpDic = trans.GetObject(Did, OpenMode.ForWrite) as Group;
       ObjectIdCollection Eids = new ObjectIdCollection();

       foreach (ObjectId id in EntGrpDic.GetAllEntityIds())
       {
        if (!id.IsErased)
        {
         DBObject dbObject = trans.GetObject(id, OpenMode.ForWrite);
         dbObject.Erase();
        }
       }
       GrpDic.UpgradeOpen();
       GrpDic.Erase();
       editor.WriteMessage("\nDeleted Group: < " + Strline + " > \r");
       editor.WriteMessage("\nCommand:\r");
      }
      trans.Commit();
     }
    }
   }
  }


BillZndl

  • Guest
Re: Question about disposing.
« Reply #16 on: October 27, 2009, 06:48:21 AM »
Here is how I been calling my methods:

That's fine, you just didn't show us the caller of your function.

I'm glad you asked because it seems like after I added this delete and another one called explode,
my crash problems started. But it seems to crash even when I don't invoke them.

Here's how I call:

Code: [Select]
private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    RUSureDelete newRUSure = new RUSureDelete();
                    newRUSure.Visible = true;                   
                }
                else
                {
                    MessageBox.Show("Please select an item on the list:");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("RUSureDelete: " + ex.StackTrace);
            }
        }

And heres the partial class form that has 2 buttons "Yes" & "No".

Code: [Select]
public partial class RUSureDelete : Form
    {
        public RUSureDelete()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.Hide();
                DeleteGroup group = new DeleteGroup();
                group.RemoveGroup(PartMonitor.Instance.GetSelectedItem());
                PartMonitor.Instance.FillListView();
                PartMonitor.Instance.SetLastSelected();

            }
            catch (System.Exception exception)
            {
                MessageBox.Show("Delete Group: " + exception);
            }

        }
    }


Quote
have a look at this code and see if it works for you

I sure will.
Thanks!


BillZndl

  • Guest
Re: Question about disposing.
« Reply #17 on: October 27, 2009, 07:27:51 AM »
Quote
have a look at this code and see if it works for you

I sure will.
Thanks!


Worked great.
I did have to change GrpDic.UpgradeOpen();
                             GrpDic.Erase();

To:     EntGrpDic.Erase();

BUT... All I did was copy a couple of my groups, then delete them and on exit (and sometimes opening another drawing) KaBoom!   :-(


BillZndl

  • Guest
Re: Question about disposing.
« Reply #18 on: October 27, 2009, 02:23:52 PM »
FWIW:
I have VS2008 express set up here so I updated my solution, keeping the target as net 2.0,
It went through without a hitch and reported converting only 2 of the files (the .sln and .csproj).
Showed no errors converting but does same thing when I load it, bring it up, then exit.

I'm about out of ideas. Everything looks good to me but then again, I don't know alot.  :laugh:


Glenn R

  • Guest
Re: Question about disposing.
« Reply #19 on: October 27, 2009, 04:19:10 PM »
I've got some time on my hands at the moment, so post your solution, as the error might be generated from elsewhere in your code  and I will have a look at it.

BillZndl

  • Guest
Re: Question about disposing.
« Reply #20 on: October 27, 2009, 04:57:24 PM »
Thanks Glenn.

I sent you the zip file via PM.

Glenn R

  • Guest
Re: Question about disposing.
« Reply #21 on: October 27, 2009, 05:12:09 PM »
Do you have any specific steps that I can follow, to reproduce the fatal error?

Glenn R

  • Guest
Re: Question about disposing.
« Reply #22 on: October 27, 2009, 05:32:28 PM »
That's a fairly large project and more than I expected. Whilst I am getting my head around it, I would suggest extracting portions of it to separate projects and testing them individually to see if you can nail down the error.

Glenn R

  • Guest
Re: Question about disposing.
« Reply #23 on: October 27, 2009, 05:42:47 PM »
First thing:

Code: [Select]
Group grp = (Group)trans.GetObject(oid, OpenMode.ForRead) as Group;

You don't need a direct cast here, because you're using 'as' - stick to one or the other.

Glenn R

  • Guest
Re: Question about disposing.
« Reply #24 on: October 27, 2009, 05:46:54 PM »
Second thing:

Code: [Select]
grp.Name = string.Concat("~Copy_", grp.Name.Substring(1, 2), DateTime.Now.Millisecond.ToString());

I would use '+' to join the strings; string.Format(), or a stringbuilder.

Glenn R

  • Guest
Re: Question about disposing.
« Reply #25 on: October 27, 2009, 05:52:58 PM »
Third thing:

RUSureDelete and RUSureExplode forms are merely asking to confirm deletion - these are superfluous; use a MessageBox instead.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Question about disposing.
« Reply #26 on: October 27, 2009, 06:42:21 PM »
First thing:

Code: [Select]
Group grp = (Group)trans.GetObject(oid, OpenMode.ForRead) as Group;

You don't need a direct cast here, because you're using 'as' - stick to one or the other.

This was my bad, editing code without coffee  :ugly:

BillZndl

  • Guest
Re: Question about disposing.
« Reply #27 on: October 28, 2009, 07:01:17 AM »
Do you have any specific steps that I can follow, to reproduce the fatal error?

Lately, all I have to do is netload, type in "Partmon" command in AutoCAD,
open drawing with several part groups in it (SDI = 0) and then exit AutoCAD, KaBoom.

That's a fairly large project and more than I expected. Whilst I am getting my head around it, I would suggest extracting portions of it to separate projects and testing them individually to see if you can nail down the error.

That was my next step. I may have to strip it down to the main form and try building it back up.
I know I'm running before I can walk here but I've taken time to do research and have tested everything
before adding more to the code. Somehow this error sort of snuck up on me.

Thanks for the tips, we're using a previous release .dll of this (which doesn't crash, AFAICT)
this new version is pretty much complete to what we want it to do so I'd like to get it up & running.  

Always something huh?

First thing:
Code: [Select]
Group grp = (Group)trans.GetObject(oid, OpenMode.ForRead) as Group;
You don't need a direct cast here, because you're using 'as' - stick to one or the other.
This was my bad, editing code without coffee  :ugly:

Ah, that answers a question I asked previously.  ^-^

Thanks again.

*Edit: At least you all didn't say "what a mess, you call that code?"   :laugh:
kinda reinforces that I'm at least on the right road.  ;-)









« Last Edit: October 28, 2009, 07:12:45 AM by BillZndl »

BillZndl

  • Guest
Re: Question about disposing.
« Reply #28 on: October 28, 2009, 11:23:48 AM »
This is beginning to look like a timing issue.
I remmed out a couple of my last methods like DisplayPreview() and was working just with copying groups
with the native AutoCAD command. It was crashing when commandEnded.
The DatabaseAppend stores the ObjectId of the groups in the GlobalModule ObjectIDCollection and on the commandEnded, for copy,
the AddGroupDate() reads the OIDCollection, opens the group, checks the name, adds date etc., returns an Arraylist of the info to the AddLVItems() for a new listview item.

After copy, it was crashing so I added an editor.WriteMessage to check the names and a threadSleep(1000) to hopefully see it before it crashed,
it started working and now and with just the threadSleep, the crash no longer occurs.   :-o

Looks like it's back to the drawing board on this listView handling.
I'll probably have to re-order some things. Dunno.

« Last Edit: October 28, 2009, 11:27:06 AM by BillZndl »

BillZndl

  • Guest
Re: Question about disposing.
« Reply #29 on: October 28, 2009, 12:23:10 PM »
I did find one error.
My naming convention for anonymous groups was trying to use duplicate names
for different groups. Using the milleseconds for unique names wasn't the answer.
The loop was going too fast for that. Must've been less than a millesecond!

I'm sure i'm not out of the woods yet but at least it's progress.  :-)