private void UpdateMemoryAnalysisGridForSelectedSymbol()
        {
            lock (this)
            {
                iStopPopulatingGrid = false;
            }
            //
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                //
                if (iListView.SelectedIndices.Count > 0)
                {
                    MemObjStatisticalData data = iParser.Data;
                    int selectedIndex          = iListView.SelectedIndices[0];

                    ListViewItem selectedItem = iListView.SelectedItems[0];
                    if (selectedItem.Tag != null)
                    {
                        MemObjStatisticalCollection collection = (MemObjStatisticalCollection)selectedItem.Tag;
                        MemOpBase baseObject = collection[0];
                        System.Diagnostics.Debug.Assert(baseObject is MemOpAllocation);
                        MemOpAllocation memObj = (MemOpAllocation)baseObject;
                        //
                        string symbolText = "Unknown";
                        if (memObj.LinkRegisterSymbol != null && memObj.LinkRegisterSymbol.Symbol != null)
                        {
                            symbolText = memObj.LinkRegisterSymbol.Symbol.ToString();
                        }
                        iMemAnalysisDetailedInfoForSymbolGroupBox.Text = @"Detailed Analysis for Symbol '" + symbolText + @"'";
                        //
                        PopulateTableRows(collection);
                    }
                }
            }
            catch (Exception)
            {
                iGridIsDirty = false;
            }
            finally
            {
                Cursor.Current      = Cursors.Default;
                iStopPopulatingGrid = false;
                //iListView.Enabled = false;
                //iListView.Select();
            }

            lock (this)
            {
                iGridIsDirty = false;
            }
        }
        private void iGrid_CellClick(object sender, XPTable.Events.CellMouseEventArgs e)
        {
            if (e.Cell.Tag != null && e.Cell.Tag is MemOpBase)
            {
                // Get the object & collection
                MemOpBase currentObject = (MemOpBase)e.Cell.Tag;
                MemObjStatisticalCollection collection = (MemObjStatisticalCollection)currentObject.Collection;

                // If we have a linked item, then we'll attempt to show it
                if (currentObject.Link != null)
                {
                    MemOpBase linkedOp = currentObject.Link;

                    // We know that the linked item should be in the same collection - so search for it.
                    int rowIndex = 0;
                    foreach (XPTable.Models.Row row in iGrid.TableModel.Rows)
                    {
                        // The first row's tag has been setup to point to a MemOpBase object for the entire row.
                        MemOpBase rowTagEntry = (MemOpBase)row.Tag;
                        if (rowTagEntry.CellAddress == linkedOp.CellAddress &&
                            rowTagEntry.OperationIndex == linkedOp.OperationIndex &&
                            rowTagEntry.AllocationNumber == linkedOp.AllocationNumber &&
                            rowTagEntry != currentObject)
                        {
                            // This is the one to focus to.
                            iGrid.TableModel.Selections.SelectCell(rowIndex, 5);
                            iGrid.EnsureVisible(rowIndex, 5);
                            iGrid.Select();
                            break;
                        }

                        // No match...
                        ++rowIndex;
                    }
                }
            }
        }
        private void PopulateTableRows(MemObjStatisticalCollection aCollection)
        {
            // Clear existing content
            iGrid.BeginUpdate();
            iGrid.TableModel.Rows.Clear();
            iGrid.Tag = aCollection;

            // Make new content
            int count = aCollection.Count;

            for (int i = 0; i < count; i++)
            {
                // The entry we are rendering
                MemOpBase baseObject = aCollection[i];

                // Only initialised if we are dealing with an allocation (or realloc) type cell.
                MemOpAllocation memObj = null;

                // The color format for the entire row.
                System.Drawing.Color rowColor = Color.Black;

                // The row we are creating
                XPTable.Models.Row row = new XPTable.Models.Row();

                // Set tag for the row
                row.Tag = baseObject;

                // Common items
                // ============
                XPTable.Models.Cell opIndexCell = new XPTable.Models.Cell(baseObject.OperationIndex.ToString("d6"));
                row.Cells.Add(opIndexCell);
                XPTable.Models.Cell lineNumberCell = new XPTable.Models.Cell(baseObject.LineNumber.ToString("d6"));
                row.Cells.Add(lineNumberCell);
                XPTable.Models.Cell cellAddressCell = new XPTable.Models.Cell(baseObject.CellAddress.ToString("x8"));
                row.Cells.Add(cellAddressCell);
                XPTable.Models.Cell functionCell = new XPTable.Models.Cell(" " + baseObject.FunctionName);
                row.Cells.Add(functionCell);

                // Row Color & Object Association
                // ==============================
                if (baseObject is MemOpAllocation)
                {
                    // Allocation
                    memObj   = (MemOpAllocation)baseObject;
                    rowColor = Color.Blue;
                }
                else if (baseObject is MemOpFree)
                {
                    // Deallocation
                    if (baseObject.Link != null)
                    {
                        memObj = (MemOpAllocation)baseObject.Link;
                    }
                    else
                    {
                        memObj = null;
                    }
                    rowColor = Color.Green;
                }
                else if (baseObject is MemOpReallocation)
                {
                    // Reallocation
                    if (baseObject.Link != null)
                    {
                        memObj = (MemOpAllocation)baseObject.Link;
                    }
                    else
                    {
                        memObj = null;
                    }
                    rowColor = Color.Purple;
                }

                // Allocation size
                // ===============
                string allocationSize = "???";
                if (memObj != null)
                {
                    allocationSize = memObj.AllocationSize.ToString();
                }
                row.Cells.Add(new XPTable.Models.Cell(allocationSize + "  "));

                // Heap size
                // =========
                row.Cells.Add(new XPTable.Models.Cell(baseObject.HeapSize.ToString() + "  "));

                // Associated object
                // =================
                MemOpAllocation symbolObject = memObj;
                if (memObj != null && baseObject.Link != null)
                {
                    // If we have an associated link item, we can connect the two items together
                    string associatedText = string.Empty;
                    if (baseObject.IsAllocationType)
                    {
                        associatedText = "Free'd by op #:  " + baseObject.Link.OperationIndex.ToString("d5");
                    }
                    else if (baseObject.IsReallocationType)
                    {
                        associatedText = "First alloc'd by op #: " + baseObject.Link.OperationIndex.ToString("d5");
                        symbolObject   = (baseObject.Link as MemOpAllocation);
                    }
                    else
                    {
                        associatedText = "Alloc'd by op #: " + baseObject.Link.OperationIndex.ToString("d5");
                    }

                    // We store the object with the cell so that we can handle hyperlinks between
                    // associated objects.
                    XPTable.Models.Cell associatedCell = new XPTable.Models.Cell(associatedText);
                    associatedCell.Tag = baseObject;

                    // Make it look like a hyperlink
                    associatedCell.Font = new Font(iGrid.Font.FontFamily.Name, iGrid.Font.SizeInPoints, System.Drawing.FontStyle.Underline);

                    // Add the cell to the row
                    row.Cells.Add(associatedCell);
                }
                else
                {
                    if (baseObject.IsAllocationType)
                    {
                        if (memObj != null)
                        {
                            symbolObject = memObj;
                        }

                        rowColor = Color.Red;
                        row.Font = new System.Drawing.Font(iGrid.Font.FontFamily.Name, iGrid.Font.SizeInPoints, System.Drawing.FontStyle.Regular);
                        row.Cells.Add(new XPTable.Models.Cell("Object never free'd!"));
                    }
                    else
                    {
                        row.Cells.Add(new XPTable.Models.Cell("???!"));
                    }
                }

                // Set row color
                // =============
                row.ForeColor = rowColor;

                // Add row
                // =======
                iGrid.TableModel.Rows.Add(row);

                // Event handling
                // ==============
                if (i % 100 != 0)
                {
                    Application.DoEvents();
                }
                lock (this)
                {
                    if (iStopPopulatingGrid)
                    {
                        break;
                    }
                }
            }

            // If no items, then dim table
            iGrid.Enabled = (count > 0);
            iGrid.EndUpdate();
        }
        private void PrepareMemoryAnalysisData()
        {
            try
            {
                Cursor.Current    = Cursors.WaitCursor;
                iListView.Enabled = false;

                long totalAllocCount = 0;
                long totalFreeCount  = 0;
                long totalAllocSize  = 0;
                long totalFreeSize   = 0;
                long totalNetSize    = 0;

                // Ensure that each allocation-symbol is added to the symbol listbox
                MemObjStatisticalData data = iParser.Data;
                //
                iListView.BeginUpdate();
                int count = data.CollectionCount;
                for (int i = 0; i < count; i++)
                {
                    MemObjStatisticalCollection collection = data.CollectionAt(i);
                    if (collection.Count > 0)
                    {
                        System.Diagnostics.Debug.Assert(collection[0] is MemOpAllocation);
                        MemOpAllocation memObj = (MemOpAllocation)collection[0];
                        //
                        long allocCount   = collection.AllocationCount;
                        long freeCount    = collection.DeallocationCount;
                        long allocSize    = collection.TotalAmountOfAllocatedMemory;
                        long freeSize     = collection.TotalAmountOfDeallocatedMemory;
                        long netAllocSize = collection.TotalMemoryAllocatedButNotFreed;
                        //
                        ListViewItem item = new ListViewItem("");
                        item.Tag = collection;
                        item.SubItems.Add(allocCount.ToString());
                        item.SubItems.Add(freeCount.ToString());
                        item.SubItems.Add(allocSize.ToString());
                        item.SubItems.Add(freeSize.ToString());
                        item.SubItems.Add(netAllocSize.ToString());
                        string symbolText = "Unknown";
                        if (memObj.LinkRegisterSymbol != null && memObj.LinkRegisterSymbol.Symbol != null)
                        {
                            symbolText = memObj.LinkRegisterSymbol.Symbol.ToString();
                        }
                        item.SubItems.Add(symbolText);
                        iListView.Items.Add(item);

                        // Update totals
                        totalAllocCount += allocCount;
                        totalFreeCount  += freeCount;
                        totalAllocSize  += allocSize;
                        totalFreeSize   += freeSize;
                        totalNetSize    += netAllocSize;
                    }

                    if (count % 100 != 0)
                    {
                        Application.DoEvents();
                    }
                }
                iListView.EndUpdate();

                // Make the first item selected
                if (iListView.Items.Count > 0 && iListView.SelectedIndices.Count == 0)
                {
                    // Add total item
                    iListView.Items.Add(new ListViewItem(""));
                    //
                    ListViewItem totalItem = new ListViewItem("Totals:");
                    totalItem.SubItems.Add(totalAllocCount.ToString());
                    totalItem.SubItems.Add(totalFreeCount.ToString());
                    totalItem.SubItems.Add(totalAllocSize.ToString());
                    totalItem.SubItems.Add(totalFreeSize.ToString());
                    totalItem.SubItems.Add(totalNetSize.ToString());
                    totalItem.SubItems.Add("");
                    iListView.Items.Add(totalItem);
                    //
                    iListView.Items[0].Selected = true;
                    iListView.Select();
                }
            }
            finally
            {
                Cursor.Current    = Cursors.Default;
                iListView.Enabled = true;
                iListView.Select();
            }
        }