1

Right, ill start from the beginning so it will be alittle easier to read through,

When ever i run my program it shows everything as it should. i can even walk around with my player for a bit, but suddenly after 10-15 seconds it just dies saying that its out of memory. i have no idea what this means as i have never had this problem before with ealier programs i have made.

here is the code the problem comes from (well that is the code i get shown)

private void Render()
     {

         dc.DrawImage(Image.FromFile("Graphics/WorldAreas/Starting-room.png"), 0, 0);


         foreach (GameObject go in gameWorld)
         {
             go.Render(dc);
         }

         bg.Render();
     }

After the few seconds it highlights "dc.DrawImage" and says that OutOfMemoryException was unhandled.

before i rewrote this i saw that someone asked for the gameobject so ill add the code for that as well

public abstract class GameObject
{


    protected PointF position; //X,Y Position in the Game World
    public PointF Position
    {
        get { return position; }

    }
    protected Image sprite;

    public virtual void Move(float factor)
    {

    }
   public virtual void Render(Graphics dc)
   {
       dc.DrawImage(sprite, position.X, position.Y, sprite.Width, sprite.Height);
       //dc.DrawRectangle(new Pen(Color.Red), CollisionRectangle);
   }


}

hopefully this was a better explaination. and hopefully someone can see the error that i cannot.

thanks in advance :)

4
  • Can you post the code for the GameObject class as this is where the exception is happening and remove the wall of code you have posted. Commented Feb 2, 2011 at 10:02
  • 5
    I'd not load Starting-room.png from disk in every frame Render for a start. Commented Feb 2, 2011 at 10:08
  • 3
    Simply posting a stack trace and some source and saying "fix this" is really poor form. Commented Feb 2, 2011 at 10:47
  • i apologuise, i have been looking everywhere for answeres and i just thought "what the hell, ill just post the entire thing" ill rewrite it. Commented Feb 2, 2011 at 11:23

1 Answer 1

2

I think you're problem is that you are loading a PNG everytime render is called. Instead load this once, and make it a static member on the class for example.

Then, use that single reference for drawing. The reason for this is that Image implements IDisposable so it doesn't release it's resources (memory) after it goes out of scope. If you were to leave it in the Render you'd want to call Image.Dispose() or use a using(Image img = Image.FromFile(..)). But I'd still go for the load it once to reduce I/O.

 private static Image StartRoomImage = Image.FromFile("Graphics/WorldAreas/Starting-room.png");
 private void Render()
 {

     dc.DrawImage(StartRoomImage , 0, 0);

     foreach (GameObject go in gameWorld)
     {
         go.Render(dc);
     }

     bg.Render();
 }
Sign up to request clarification or add additional context in comments.

5 Comments

when i try that i get errors with "StartRoomImage" "The type or namespace named 'StartRoomImage' could not be found (are you missing a using directive or an assembly reference?)"
Sorry, I missed an 'Image' out.. Check it now.
Also if i do it like that will i need to do the same for everything else i add in the render? because i am going to make this multileveled so that when the player exits one room, another background of another room loads. Also thank you so much for taking the time to answere this, i have been stuck for a while :)
It might then be better (depending on the number of rooms) to load all the images up into a Dictionary<Guid, Image>. Assign each room a Guid, and locate the appropriate image in that dictionary.
thank you so much ian ^^ feels so great to have this error fixed, now i can continue working ^^ you were a great help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.