Image.tonamedmatrix() crashycrashy?

Sep 12, 2011 at 12:58pm

Image.tonamedmatrix() crashycrashy?

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]);
		img.tonamedmatrix(this.matrix.name);
		}
	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?

#58852
Sep 12, 2011 at 1:26pm

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

#211514
Sep 13, 2011 at 9:10am

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?

#211515
Sep 13, 2011 at 12:00pm

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) {
		test.close();
		result = new Image(fn);
		}
	else error("Utils.SafeNewImage(): file doesn't exist", fn, "n");

	return result;
	};
#211516

You must be logged in to reply to this topic.