2

I have been receiving the following warning/error message on my game after it has been running for 10-15 seconds, this even occurs when there is no interaction with the game:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Drawing.dll

Additional information: The operation completed successfully

I was wondering if I needed to call a Dispose() method in order to remove the objects that are not being used as they are being loaded even when they have been removed from the game world?

I have all objects stored as a list (shown below)

class Items
{
    public static List<Obj> objList = new List<Obj>();

    public static void Initialize()
    {
        //The Player
        objList.Add(new Player(new Vector2(50, 50)));
        //The Enemies
        objList.Add(new Enemy(new Vector2(500, 400)));
        objList.Add(new Enemy(new Vector2(600, 200)));
        //The Collectibles 
        objList.Add(new BlueBall(new Vector2(300, 400)));
        objList.Add(new GreenBall(new Vector2(350, 100)));
        objList.Add(new OrangeBall(new Vector2(65000, 250)));
        objList.Add(new PinkBall(new Vector2(100, 400)));
        objList.Add(new RedBall(new Vector2(600, 400)));
        objList.Add(new YellowBall(new Vector2(500, 250)));

    }

I then call Items.Initialize in the Game1 class

I currently have a collision method as well that is called once an object collides with another and I am thinking that this may be causing the issue but I am not 100%. As my game stands, I am currently only setting the objects state to = alive = false; in order to 'kill' them and remove them from the screen, this can be seen below with an example showing the collision of the Player and an Enemy:

 //Collision with enemy
            Enemy enemy = CheckCollisionAgainst<Enemy>();
            if (enemy != null)
            {
                gameOver.Play();
                alive = false;
            }

I've been stuck with this error now and have not been able to find a solution to stop it from happening. As I stated earlier, it occurs after the game has been running for 10-15 seconds so I am unable to progress with any other features until this error is fixed.

I appreciate any help and thanks in advance.

Community
  • 1
  • 1
KizzaWellz
  • 35
  • 7
  • "Additional information: The operation completed successfully" This dosen't looks like anything is going wrong, it looks like it is actually throwing you an error just to tell you that everything is ok. Your game freezes besause there is no error-handling in your code. – Antoine Pelletier Jul 02 '15 at 17:27
  • @AntoinePelletier ah okay, I see! What will I need to put in in order to add error handling? – KizzaWellz Jul 02 '15 at 17:28
  • He... well... You probably know what a try catch is, but I don't think it's the best thing to do, I personnaly would put a try catch statement at the very place things seem to go wrong https://msdn.microsoft.com/en-us/library/seyhszts(v=vs.110).aspx But it si not the best you can do. the best would be that you have a whole class that catches every errors and react to them spesificly. I'm gonna try to find something for you. – Antoine Pelletier Jul 02 '15 at 17:42
  • @AntoinePelletier Yes I am fond of try catch (place them around code and log to somewhere). If you could find something that would be great! – KizzaWellz Jul 02 '15 at 17:56

1 Answers1

2

I found some interesting infos, looks like try catch won't do anything for you, as it is probably the amount of graphics you are asking c# to generate. You have to lower the quality of the graphics your displaying or reducing the amount of objects that require graphics in your code, see :

System.ComponentModel.Win32Exception: The operation completed successfully

Seems like your not the only one with this problem.

People generaly have high graphics demands just before having the same exeption, Optimizing your code is what you need, managing to reduce memory needed for your game is not a simple task thought. Courage my friend !

ps. starting by dispose() would be a great idea ;)

Community
  • 1
  • 1
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
  • I have been trying Dispose() in my code in my Items class where the list of objects is but I cannot seem to get it to work – KizzaWellz Jul 06 '15 at 11:09
  • Remember to always create the fewest objects you can, and delete them as soon as they are not useful. Remember that your problem is graphic related, so try to reduce the amount of graphics you'r generating. If you'r game really needs a lots of graphics, then you'll have to learn about GPU... Ho and you may have to dispose of your object one by one exept if you can destroy the whole table at once, but before, try .Delete() . – Antoine Pelletier Jul 06 '15 at 12:50
  • The list that I currently show in my post is the least amount of objects I need within my game. So say that I have a collision method, I will call Delete() within the collision method in order to destroy the object? – KizzaWellz Jul 06 '15 at 14:08
  • If the object is not useful anymore or should be deleted after the colision, then yeah, remove it imediately – Antoine Pelletier Jul 06 '15 at 14:26
  • I have `Player player = CheckCollisionAgainst(); if (player != null)` then Dispose() but the error still seems to be showing. Will I need to include some sort of error-handling? – KizzaWellz Jul 06 '15 at 14:29
  • If the error is the same as before, try catch won't help. I recommend you, just as a test, to try to generate the most basic things first, maybe only one object to see if the game works for longer than 10 secondes. If you see that the games work well with it, add another objects, one by one until you reach the "too much graphics generating". – Antoine Pelletier Jul 06 '15 at 14:33
  • I just tried that and the error occurred after just 2 objects (+ a background) – KizzaWellz Jul 06 '15 at 14:48
  • It ocured randomly or because of the colision ? – Antoine Pelletier Jul 06 '15 at 14:51
  • randomly after playing for 10-20 seconds (note: the objects were the player and an enemy) – KizzaWellz Jul 06 '15 at 14:52
  • Be sure about that one thing for optimizing, creating objects into a loop or a colision test is a bad practice, that the first thing you need to look for. – Antoine Pelletier Jul 06 '15 at 15:00
  • I have my objects in a list and a loop is used to call the objects to see whether they are needed to be drawn or not and the collision is purely there in order to remove the objects – KizzaWellz Jul 06 '15 at 15:50
  • I can't take a look at your code, you'r creating a game i understand what it is. I could help you optimizing, but then again, that would require me to see your code, wich is not very acceptable. Optimizing is complicated. Here is one thing that i already see about your code above : The function CheckCollisionAgainst should not return a complete ennemy, it should return just "true" if there were any colision, if you need the id of the ennemy that colided, return just is ID, not the whole thing, and with changes like that, you will slowly reduce the calculation you'r demanding to your CPU. – Antoine Pelletier Jul 06 '15 at 16:01
  • Thanks for your help anyway, I'm new to programming so I will have to face optimizing at some point haha, I'll see what I can do – KizzaWellz Jul 06 '15 at 16:17