Problem using FXFileStream

Hi,

I’m having problems using FXFileStream in FXRuby. I want so save an
FXImage using the “save” method.
Here’s the piece of code:

FXFileStream.open (“test.jpg”, FXStreamSave) do |stream |
img.savePixels(stream)
end

This actually works, but the file is not closed until I quit the
application. After “end” the file is still open and no data has been
written to it. When I quit the application the file is a valid .jpg
file. Is this a bug or is there anything wron with my code?

Thanks for any help,

Thomas

Thomas Stammeier wrote:

I’m having problems using FXFileStream in FXRuby. I want so save an
FXImage using the “save” method. Here’s the piece of code:

FXFileStream.open (“test.jpg”, FXStreamSave) do |stream |
img.savePixels(stream)
end

This actually works, but the file is not closed until I quit the
application. After “end” the file is still open and no data has been
written to it. When I quit the application the file is a valid .jpg
file. Is this a bug or is there anything wrong with my code?

I don’t see anything wrong with your code, but I also don’t understand
how you could be seeing the behavior you describe. For reference, here’s
the code that implements the FXFileStream.open singleton method (from
lib/fox/iterators.rb):

 def FXFileStream.open(filename, save_or_load, container=nil)
   fstream = FXFileStream.new(container)
   status = fstream.open(filename, save_or_load)
   if block_given?
begin
       yield fstream
ensure
  fstream.close
end
   else
     fstream
   end
 end

When you use FXFileStream.open as shown, there’s an ‘ensure’ block that
closes the file stream (and this calls the standard C library’s fclose()
function under the hood). The only possible problem I see (and one I
should account for) is that FXFileStream#open (the instance method)
returns some kind of error code. But if that were to happen, I don’t
think that call to FXImage#savePixels would have any effect, i.e. you
wouldn’t end up with a valid JPEG file at the end.

Lyle

Lyle Johnson wrote:

I don’t see anything wrong with your code, but I also don’t understand
how you could be seeing the behavior you describe.

This is exactly what I was wondering about.

Could this problem be related to the operating system in any way? I
forgot to mention I’m running ruby on Windows 2000.

A simple way to see this problem is entering the following code in irb
(using Windows):

require 'fox’
include Fox
FXFileStream.open (“test”, FXStreamSave) { |stream| }

Then trying to delete the file with the Explorer will raise a “sharing
violation” (I can’t give you the exact error message, I have a German
Windows installed).
This seems odd to me as the file should be closed after the block.

Maybe someone can check if this is happening on your Ruby as well?

Thanks,
Thomas

Thomas Stammeier wrote:

Could this problem be related to the operating system in any way? I
forgot to mention I’m running ruby on Windows 2000.

A simple way to see this problem is entering the following code in irb
(using Windows):

require 'fox’
include Fox
FXFileStream.open (“test”, FXStreamSave) { |stream| }

Then trying to delete the file with the Explorer will raise a “sharing
violation” (I can’t give you the exact error message, I have a German
Windows installed).
This seems odd to me as the file should be closed after the block.

Maybe someone can check if this is happening on your Ruby as well?

Yes, I just confirmed this under Windows 2000 as well, but I still don’t
understand why it’s happening. I’m going to add it to the bug list and
investigate further.