Click here to Skip to main content
15,881,044 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My program loops through an array of null elements, assigning each element to a new object (an instance of a class I made). The array gets quite large - about 60,000 elements in size. On occasion, this loop will work, but generally the program will crash, giving a generic microsoft error "x.exe has encountered a problem and needs to close...". I have concluded that this is not a memory size issue, but could be a memory management issue of some sort...

Here is the Point class:

public partial class Point : System.Windows.Window
    {
        private string m_ID; // Theoretically to be used only for the transmitter point locations... Could be used for other points too depending on application...
        public double x, y, power;
        public string m_time;
        public Point()
        {
            power = 0;
        }
        public Point(double x, double y)
        {
            this.x = x; this.y = y; power = 0;
        }
        public Point(double x, double y, int column)
        {
            this.x = x; this.y = y; power = 0;
        }
        public Point(double x, double y, int column, string time)
        {
            m_time = time; this.x = x; this.y = y; power = 0;
        }
        public Point(double x, double y, int column, string time, double power)
        {
            m_time = time; this.x = x; this.y = y; this.power = power;
        }
        /// <summary>
        /// THE LABEL OR NAME OF THE GIVEN POINT.
        /// </summary>
        public string ID
        {
            get
            {
                return m_ID;
            }
            set
            {
                m_ID = value;
            }
        }
        public string time
        {
            get
            {
                return m_time;
            }
            set
            {
                m_time = value;
            }
        }
    }


And here is the function which builds the array of Point's:

public Point[] getPoints(ExcelInterface excelData)
        {
            int longColumn = excelData.FindColumnNumber("Longitude");
            int latColumn = excelData.FindColumnNumber("Latitude");
            int timeColumn = excelData.FindColumnNumber("Time");
            int powerColumn = excelData.FindColumnNumber("Power_dBm");
            Point[] dataPoints = new Point[excelData.rows];
            string hh, mm, ss, MM, dd;
            hh = string.Empty; mm = string.Empty; ss = string.Empty; MM = string.Empty; dd = string.Empty;
            int hour, minute, second;
            double power;
            DateTime time;
            double[] thresholds = getPowerThresholds(numThresholds, excelData);
            string dateTime, timeString;
            if (System.DateTime.Now.Month < 10)
                MM = "0" + System.DateTime.Now.Month.ToString();
            else MM = System.DateTime.Now.Month.ToString();
            if (System.DateTime.Now.Day < 10)
                dd = "0" + System.DateTime.Now.Day.ToString ();
            else dd = System.DateTime.Now.Day.ToString();
            string date = System.DateTime.Now.Year + "-" + MM + "-" + dd;
            double lat, lon;
            // array stop between 1900 and 2000 in size...
            for (int i = 2; i < excelData.rows; i++)
            {
                if (i % 400 == 0)
                {
                }
                time = new DateTime(); // DEBUG
                lat = (double)excelData.GetCellValue(i, latColumn, 1);
                lon = (double)excelData.GetCellValue(i, longColumn, 1);
                time = (DateTime)excelData.GetCellValue(i, timeColumn, 1);
                power = (double)excelData.GetCellValue(i, powerColumn, 1);
                hour = time.Hour;
                if (hour < 10)
                    hh = "0" + hour.ToString();
                else hh = hour.ToString();
                minute = time.Minute;
                if (minute < 10)
                    mm = "0" + minute.ToString();
                else mm = minute.ToString();
                second = time.Second;
                if (second < 10)
                    ss = "0" + second.ToString();
                else ss = second.ToString();
                timeString = "T" + hh + ":" + mm + ":" + ss + "Z";
                dateTime = date + timeString;
                try
                {
                    dataPoints[i - 2] = new Point(lon, lat, i, dateTime);  
                }
                catch (Exception ex)
                {
                }
            }


I have also tested this by simplifying the loop where the error occurrs, by using only the constructor:

   for (int i = 2; i < excelData.rows; i++)
            {
                try
                {
                    dataPoints[i - 2] = new Point(0, 0, 0, "");  
                }
                catch (Exception ex)
                {
                }
}


The program still crashed with this test scenario. I have also built a simple test program which solely creates a point array and assigns each array element to a new Point. That program worked just fine with over 1million elements...

I would really appreciate any help - Ive been wrestling with this for days!

Rich
Posted
Updated 20-Jul-10 4:51am
v2
Comments
Alan N 16-Jul-10 15:17pm    
Is that really an empty catch block within the loop?
OriginalGriff 16-Jul-10 15:23pm    
If your program crashes with only the for, try-catch and Point constructor, try moving the excelData.rows out of the loop - assign it to an int before the loop, and test against the int. It may be that the property is causing the problem by counting the rows badly.
Oh, and as Alan N says - don't use empty catch blocks: at the minimum log a problem so you know it has occurred.

Erm...is there any reason your inheriting from System.Windows.Window as I suspect this is cause of your current issue?

You could store your point information just as easily in a lightweight struct. You could also eliminate alot of the string operations around the date stuff...

I don't usually like just giving out answers, but there is a lot of excess compexity in your example, will this do the job?

C#
public struct Point
{
	public double Longitude{get;set;}
	
	public double Latitude{get;set;}
	
	public double Power{get;set;}
	
	public DateTime Time{get;set;} 
}
	
public Point[] getPoints(ExcelInterface excelData)
{
    int longColumn = excelData.FindColumnNumber("Longitude");
    int latColumn = excelData.FindColumnNumber("Latitude");
    int timeColumn = excelData.FindColumnNumber("Time");
    int powerColumn = excelData.FindColumnNumber("Power_dBm");
    Point[] dataPoints = new Point[excelData.rows];
  	DateTime date = DateTime.Now.Date()
    double[] thresholds = getPowerThresholds(numThresholds, excelData); // Don't know what this is for
    double lat, lon, power;
    // array stop between 1900 and 2000 in size...
	List<Point> points = new List<Point>();
    for (int i = 2; i < excelData.rows; i++)
    {
		Point p = new Point
		{
			Longitude =  (double)excelData.GetCellValue(i, longColumn, 1)
			,Latitude =  (double)excelData.GetCellValue(i, latColumn, 1)
			,Time = date.Add(((DateTime)excelData.GetCellValue(i, timeColumn, 1)).TimeOfDay)
			, Power =  (double)excelData.GetCellValue(i, powerColumn, 1)
		}
		points.Add(p)
    }
	return points.ToArray();
}
 
Share this answer
 
v2
Comments
DaveyM69 16-Jul-10 18:14pm    
Reason for my vote of 5
Good answer
Oh, sorry, the windows inheritance I removed prior to this post. Not too sure why I did that... That was not the cause of the issue though - removing the inheritance didn't stop the error. Actually, using a list instead of an array solved it!

Now that this one is solved, I have run into another strange issue... I have a function that formats some point nodes and places these nodes into a kml. These points are displayed by google earth. While looping through the foreach loop, a generic windows error message appears and the program crashes at a random line. Actually, while I was debugging and stepping through the code, an error occurred in between steps...

Heres is the code:

C#
private void plotPoints(Point[] points, double[] thresholds)
    {
        string node;
        string style = "";

        System.Xml.XPath.XPathNavigator navigator = xDoc.CreateNavigator();
        navigator.MoveToChild("kml", "http://www.opengis.net/kml/2.2");
        navigator.MoveToFirstChild();

        if (isNewKml())
            insertFolder("Points", navigator);

        else // delete folder
        {
            deleteFolder("Points");
            insertFolder("Points", navigator);
        }

        navigator = getNavigator(navigator, "Points");

        foreach (Point p in points)
        {
            if (p != null)
            {
                if (1 % 1000 == 0)
                { }

                // Errors occur here at random places...
                // Check power thresholding and apply colors accordingly
                for (int n = 0; n < thresholds.Length; n++) // Error here
                {// error here
                    if (p.power < thresholds[n])
                    {
                        style = "CustomStyle" + (n + 1).ToString();
                        break;
                    }
                    else if (n == (thresholds.Length - 1)) // Gets the case where a power measurement is above the average max (20 highest measurements)
                        style = "CustomStyle" + (n + 1).ToString();
                }
                object[] arguments = { pointIdex, System.Environment.NewLine, p.time, System.Environment.NewLine, style, System.Environment.NewLine, p.x, p.y };

                // Error here (above line) while waiting (not stepping)!

                node = String.Format("<name>Point{0}</name>{1}<TimeStamp> <when>{2}</when> </TimeStamp>{3}<styleUrl>#{4}</styleUrl>{5}<Point> <coordinates>{6},{7}, 0</coordinates> </Point>");

                navigator.MoveToFirstChild();
                navigator.InsertElementAfter(navigator.Prefix, "Placemark", navigator.LookupNamespace(navigator.Prefix), null); // "Point" + pointIdex
                navigator.MoveToNext();
                // navigator.MoveToFirstChild(); // not used?
                navigator.AppendChild(node);
                pointIdex++;
                navigator.MoveToParent();
            }
        }
    }

}


Does anyone have any idea why this might be?
 
Share this answer
 
Comments
Toli Cuturicu 20-Jul-10 10:52am    
Reason for my vote of 1
not an answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900