1

I'm creating a tool that reads information from a file and also displays an image extracted from said file as well. it reads the info just fine and everything but when it comes to displaying the image it freezes the program without an error. the program just becomes unresponsive. the debugger doesn't say anything either except after some time it will say the thread has exited with still no response from the program.

i need to rename some stuff as i don't want to get in trouble

Here's the code I'm using to extract and display the image:

try
        {
            global.meta.filetype = STFSRead.readString_C(0, 4);
            textBox2.Text = global.meta.filetype;
            textBox3.Text = STFSRead.detectType(global.meta.contype.ToString("X4"));
            textBox4.Text = global.meta.metaversion.ToString();
            textBox5.Text = global.meta.id1;
            textBox6.Text = global.meta.version.ToString("X");
            textBox7.Text = global.meta.version2.ToString("X");
            textBox8.Text = global.meta.id2;
            textBox9.Text = global.meta.id3;
            textBox10.Text = global.meta.id4;
            textBox11.Text = global.meta.id5;
            textBox12.Text = global.meta.displayname;
            textBox13.Text = global.meta.titlename;
            textBox14.Text = STFSRead.detectSomeInfo(global.meta.aflag.ToString("X2"));
            pictureBox1.Image = STFSRead.loadImage();
        }
        catch
        {
            throw new Exception("What did you do?\n All this is suppose to do is read a file, how did you screw that up?");
        }

public static Image loadImage()
    {
        Exception failed = new Exception("LoadImage failed. Contact a developer");
        string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/appname/cache/";
        string imgname = global.meta.id.Replace(" ", "") + ".png";
        if (Directory.Exists(@path))
        {
            if (File.Exists(@path + imgname))
            {
                using (Image img = Image.FromFile(@path + imgname))
                {
                    return img;
                }
                throw failed;
            }
            else if (!File.Exists(@path + imgname))
            {
                if (extractData(imgname, 0x171A, 0x4000))
                {
                    using (Image img = Image.FromFile(@path + imgname))
                    {
                        return img;
                    }
                    throw failed;
                }
                else
                    throw failed;
            }
            else
                throw failed;
        }
        else if(!Directory.Exists(@path)){
            Directory.CreateDirectory(@path);
            if (File.Exists(@path + imgname))
            {
                using (Image img = Image.FromFile(@path + imgname))
                {
                    return img;
                }
                throw failed;
            }
            else if (!File.Exists(@path+imgname))
            {
                if (extractData(imgname, 0x171A, 0x4000))
                {
                    using (Image img = Image.FromFile(@path + imgname))
                    {
                        return img;
                    }
                    throw failed;
                }
                else
                    throw failed;
            }
            else
                throw failed;
        }
        else
            throw failed;
    }

public static bool extractData(string filename, long startpos, long length)
    {
        string data = STFSRead.readString_A(startpos, length);
        string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+"/appname/cache/";
        if (Directory.Exists(@path))
        {
            using (StreamWriter file = new StreamWriter(@path + filename))
            {
                file.Write(data);
                file.Close();
            }
        }
        else if (!Directory.Exists(@path))
        {
            Directory.CreateDirectory(@path);
            using (StreamWriter file = new StreamWriter(@path + filename))
            {
                file.Write(data);
                file.Close();
            }
        }
        if (File.Exists(@path + filename))
            return true;
        else
            throw new Exception("Couldn't extract file "+filename+" at location "+startpos+" with a length of "+length);
    }

public static string readString_A(long startpos, long length)
    {
        string s = "";
        for (long i = startpos; i < startpos + length; i = i++)
        {
            global.fs.Position = i;
            byte[] buf = new byte[1];
            global.fs.Read(buf, 0, 1);
            if (buf[0] == 0x00) // Null symbol
            {
            }
            else
            {
                char c = Convert.ToChar(buf[0]);
                s += c;
           }
        }
        if (s == "" || s == " ")
        {
            s = "";
        }
        return s;
    }

I have checked my appdata folder and while the directory gets created, the image does not get created so i'm thinking its failing during extraction. and none of my exceptions get triggered either

Edit: about the exceptions.... i added all of them when the program started screwing up to see if it will trigger them and narrow down where the error might be. i don't want it to handle those exceptions. might not be the best way to do that but i am still learning c# so i don't expect it to be the best way. if there is a better way to do this then let me know.

  • 1
    This is quite possibly the worst exception/error handling code I've ever seen. I realize that's not entirely constructive or helpful but... just wow. To claim that the program hangs "without error" in something like this isn't entirely reliable. When you debug this, where does it go wrong? Is there a specific statement on which the system hangs? Some infinite logic path? Some unhandled exception? – David Aug 02 '13 at 01:31
  • I don't know what 7 he saying – Jeff Bootsholz Aug 02 '13 at 01:36
  • please give more details about the error or exception,so everyone can make a help.. – bystander Aug 02 '13 at 01:39
  • @David, ya i know its some bad exception handling... mainly because i don't want it to handle them. i added all those exceptions to try to get it to trigger them so i can see where the error is more likely occurring. as for if the debugger says anything, no. doesn't say anything unexpected. the program just becomes unresponsive when i try loading the image. it reads everything else because i had it output that stuff in the debugger but stops when it gets to the image. – user1425470 Aug 02 '13 at 01:43
  • @user1425470: Clearly this approach to "see where the error is more likely occurring" isn't working. The approach is flawed on many levels. More to the point, when you step through this in a debugger, line by line, where does it fail? Don't just run the whole application and rely on this "error handling" code. Actually step through the code in a debugger and see where it fails. If it truly "just becomes unresponsive" and never actually reaches any of this code then the problem would have to be somewhere else. If it does reach this code, step through in the debugger and find where it fails. – David Aug 02 '13 at 01:47
  • @user1425470: `"if there is a better way to do this"` - Well, if there's an exception in the code, you don't need to manually throw one. You can allow the exception to be thrown on its own and simply catch it higher in the application logic. (Where you'd likely log it and/or display an error.) For example, your first `try/catch` block throws away whatever exception it catches and replaces it with one devoid of information. Don't throw away the exception, it contains the actual error message as well as the stack trace of where it occurred. – David Aug 02 '13 at 01:49
  • @David: Thanks for the tip. i'll start using a different way to debug it. i fixed the loop fcuesta caught and now i have an out of memory error. progress at least :) – user1425470 Aug 02 '13 at 02:00

1 Answers1

3

I think your problem is here:

for (long i = startpos; i < startpos + length; i = i++)

It should be:

for (long i = startpos; i < startpos + length; i++)

i=i++ is not actually incremeting the variable

You can find why here: i = i++ doesn't increment i. Why?. Thanks to @RenniePet for the link.

Community
  • 1
  • 1
fcuesta
  • 4,429
  • 1
  • 18
  • 13
  • wow.... i didn't see that. originally it was i = i + 2 because it was reading something else and i just took off the 2. it actually triggered one of my exceptions. now i have an outofmemory error but at least thats progress :). thanks. – user1425470 Aug 02 '13 at 01:55
  • Good catch. An infinite loop would definitely cause the observed behavior. Counterintuitively I would have thought that `i = i++` actually *would* (albeit indirectly) increment the value after the statement is executed, but it looks like that's not the case. – David Aug 02 '13 at 01:55
  • @David: Yes, infinite loops do tend to slow things down. :-) – RenniePet Aug 02 '13 at 01:58
  • @RenniePet: The answer at your link explains it pretty well. I thought the `++` is evaluated after the entire statement (therefore, after the assignment), but I guess it's evaluated *between* the right-side evaluation and the assignment. – David Aug 02 '13 at 01:59