I have a procedure that runs through a loop of all the scene materials and reads from various data files a list of attributes to apply.
None of the files individually is anywhere near 1024 bytes but when I run it in a loop, it seems my fopen and fgetline read into the same memory area. After several times through the loop, Maya crashes and tells me "Insufficient memory. Quitting Maya."
After some additional trouble shooting, I was able to produce the more informative error of "Internal file descriptor table is full." Anyone know a way around something like this?
Here you go. There is some extra stuff in there as I was debugging it. Also, I got some advice that I should try using global vars so that is why that stuff is floating around in there...
int $fileId;
$fileId = `fopen $effectDescFile "r"`;
global string $effectAttrs[];
int $num = 0;
while ( !`feof $fileId` )
{
$effectAttrs[$num] = `fgetline $fileId`;
$num += 1;
}
Like I said this works fine as a one shot, but when I run a procedure with this in a loop, it blows up. I even tried some funky stuff in which I was using an eval command to cahnge the name of the $fileId var each time through the loop, but it seems that even with that Maya is reading all the text into this file descriptor table which I am blowing out.
I think you are missing fclose $fileId; after your while loop.
Your code opens a file, does a getline in a while loop until you reach eof. At that point you have parsed the entire file. Unless you are doing something else with the file, you should probably close it.
I assume your code blows up when put in a loop because you are opening N files but never closing them.
I actually just forgot to copy that. I do have an fclose right after the while loop. Here is another version of my code where I read the file as a whole instead of line by line. It also crashes in the same way and for the same reasons when run in a loop, but works fine once at a time.
Still my original question remains. If I had a single text file greater than 1024 bytes, how can I read that in? I am hoping that will give me some insight as to a way around my problem.
According to the fread MEL docs, there is a 1024 byte limit on string data being read from a file. It supposed to only return the first 1024 bytes and then leave the rest in the file. So you would have to loop using fread and checking for eof.
If you have 50k of text that you have to read that you cannot parse line-by-line into your own data structures as you read it, I'm not sure what you would do. I guess you would have a string array of size 50 and then read each 1k chunk into a string array entry.
Can you post the code that has the loop that causes your read code to crash, since you already know that you read code works when not placed in the loop?
I have figured out my problem! And it was totally unrelated! Ugh.
My script deals with applying attributes to shaders. Somewhere along the line Maya was sticking me with this wacky attribute called .ignoredShadingConnections. I don't know anything about this damn thing, but what was happenning was if I every tried to query its value or apply the value, Maya would blow up.
Like I said, I don't know where the hell this thing comes from, but I now try to kill it in my script whenever I think it could be around. This made all my problems go away!
Thanks for your help, though. Talking with you helped keep me pounding away on it.