Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problems with RenderWindow.Capture()  (Read 4708 times)

0 Members and 2 Guests are viewing this topic.

Niall

  • Newbie
  • *
  • Posts: 18
    • View Profile
Problems with RenderWindow.Capture()
« on: October 10, 2011, 08:00:51 pm »
I've been doing some n-body simulation stuff and I've been wanting to capture images every frame so I can put them together into a video later. I'd messaged Laurent after getting some memory leak issues with the Capture() function and he helped me out - calling image.Dispose() once I'd saved it to a file did the trick.

I noticed however, when I was taking hundreds of screenshots, randomly, sometimes for only one and sometimes as a block of 10-20 frames, they were just saving as completely black. Ignored the content of my renderwindow, just.. a black image. It's a reproducable bug but the instances of it saving a black frame seem to be random.

I tried another approach (which actually helps simulation time, so I favour this one), wherein I hold a List<Image>, adding a captured image each frame. Then after however many seconds of simulating I loop through the array and save each Image to the disk, and close the window.
..However, I was running into memory leaks just from images.Add(app.Capture()); so I changed it to:
Code: [Select]
Image img = app.Capture();
images.Add(img);
img.Dispose();


I thought this'd work great, but now when it goes to loop through the images I get an error. "AccessViolationException was unhandled. Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I looked up the error online and can't seem to find a fix anywhere.

Could anybody help me out a bit? Here's a stripped down example - http://www.fileize.com/files/dea5e894/cc9/SFMLing.zip

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problems with RenderWindow.Capture()
« Reply #1 on: October 10, 2011, 10:13:50 pm »
You can't use the image once it has been disposed. After calling Dispose(), the image is destroyed and cannot be used at all.
Laurent Gomila - SFML developer

Niall

  • Newbie
  • *
  • Posts: 18
    • View Profile
Problems with RenderWindow.Capture()
« Reply #2 on: October 10, 2011, 11:09:21 pm »
Hmm, what would be an alternate method for solving this problem then, without memory leaks? I dunno how I'd approach it if I can't instance it again from an array..

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problems with RenderWindow.Capture()
« Reply #3 on: October 10, 2011, 11:12:46 pm »
Release the image when you actually don't need it anymore: after you save it.
Laurent Gomila - SFML developer

Niall

  • Newbie
  • *
  • Posts: 18
    • View Profile
Problems with RenderWindow.Capture()
« Reply #4 on: October 10, 2011, 11:15:29 pm »
But if I wait until I save it at the end of the simulation and I need to run it for a significantly long amount of time, the memory leaks get very severe and slow down the simulation. If I save them as I go, I randomly get that black image issue.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problems with RenderWindow.Capture()
« Reply #5 on: October 11, 2011, 07:43:54 am »
If you wait until the end it's not a memory leak, it's just that you accumulate a huge amount of images, which need a significant amount of memory (width * height * 4 bytes for each).

We could try to solve that black image problem. Can you show me a complete and minimal example that reproduces the problem?

And why don't you use a dedicated software for capturing videos? Using SFML functions is far from being optimal.
Laurent Gomila - SFML developer

Niall

  • Newbie
  • *
  • Posts: 18
    • View Profile
Problems with RenderWindow.Capture()
« Reply #6 on: October 12, 2011, 12:45:39 pm »
Ah, makes sense. Sorry, I have a limited understanding of memory.

Hm.. by simplifying my program down to show you the issue I seem to have resolved the issue. It still seems to randomly save out black images but I'll try and pinpoint what's causing it and get back to you with an example of it.

As for the dedicated software thing - my simulation's running on a timestep of 0.05 and I can't predict how long it'd take to compute a frame thus I can't take a video and speed it up, it could end up looking a bit laggy if certain portions irregularly take longer to move along.

Niall

  • Newbie
  • *
  • Posts: 18
    • View Profile
Problems with RenderWindow.Capture()
« Reply #7 on: October 12, 2011, 01:36:28 pm »
(Apologies for double post, just in case you miss me editing the previous one)

I've missed a trick here. I was simulating something again the old way to see if I came across any black images, and happened to tab back into it around frame 270. Turns out the renderwindow itself had gone completely black, it wasn't the SaveToFile function at fault. I don't know exactly what had triggered it as I'm making sure to clear the window every step etc etc but yes, working on it. It might only happen when the window's not in focus.

Niall

  • Newbie
  • *
  • Posts: 18
    • View Profile
Problems with RenderWindow.Capture()
« Reply #8 on: October 13, 2011, 06:32:54 pm »
Triple post, woo! On a roll!

I don't know if you'll experience the same issue, but it definitely seems to only occur when the window's minimized (possibly when out of focus, but I doubt it). The RenderWindow goes completely black and it messes with the capture function: http://www.fileize.com/files/9f261991/108/SFMLing.zip

This might be a feature, I'm not sure, but yeah.