Image.tonamedmatrix() crashycrashy?

    Sep 12 2011 | 12:58 pm
    I'm getting a lot of crashes with Image.tonamedmatrix(). I think I know why, but am not sure of the best way to avoid them.
    Consider the following code as part of a Javascript class Foo(), which reads an image file from disk and creates a Jitter Matrix from the image file, using an Image object as an intermediary.
    Foo.prototype.ReadImage = function(fn)
    	var img = new Image(fn);
    	if (img) {
    		this.matrix = new JitterMatrix(4, "char", img.size[0], img.size[1]);
    	else error("Foo.ReadImage(): invalid file name", fn, "n");
    The crashes seem to arise when ReadTexture() is called with a non-existent file name. I had hoped to catch these by testing whether the Image had been created. It appears the Image is created even if the file doesn't exist. What's the best way to catch FileNotFound errors in this situation?
    And, would it be possible for Image.tonamedmatrix() not to crash Max in this case?

    • Sep 12 2011 | 1:26 pm
      I am not so familiar with javascript, but is it possible to use an absolutepath object? this returns 'not found' when the file is not there
    • Sep 13 2011 | 9:10 am
      In Javascript it would be possible to instantiate a File object and use that to check if the filename exists.
      I still don't think the code above should crash Max.
      @Support: is the above enough to go on, or do you need a complete Javascript and patch to demonstrate the crash?
    • Sep 13 2011 | 12:00 pm
      Failing a better solution, I'm using the following as a replacement for new Image(fn):
      Utils.SafeNewImage = function(fn)
      	var test = new File(fn),	// Try to open file with given file name.
      		result = null;
      	if (test.isopen) {
      		result = new Image(fn);
      	else error("Utils.SafeNewImage(): file doesn't exist", fn, "n");
      	return result;