c# how to delete an instance after you throw it around the program?

Started by
13 comments, last by jpetrie 10 years ago

ok maybe title wasnt super clear now i explain it, is really simple:

i have a collection of items Tree that i use in various ways (game logic and interface mainly)

at some point my interface point at some member (example i want to throw a fireball and select a tree as target, then my interface show me a list of available trees and i pick one, my interface class then has a member of my collection in it), then i throw that member at my game logic to do stuff, draw it, change it etc etc

in the end it happens that member has to go, in this case the tree is burned, leave something behind and disappear

how can i tell it to delete that member?(or set it null or similar)

Advertisement

You might need to clarify your question to get a helpful answer, perhaps using slightly more technically accurate terminology to avoid any ambiguities.

If you are talking about memory management, then I guess the answer to your question is "you don't". The .Net runtime is responsible for Garbage Collection so it will delete any objects in memory which are no longer being referenced.

It's a little unclear what you are asking. If you mean, "How do I remove things from a list or collection" it depends on what kind of collection. If it's a List, you do it like so:


list_of_items.Remove(item);

The trick is, of course knowing which item you want to remove. You can also remove all of the ones that match a certain predicate:


private static bool ItemIsDead(Item i) {
   return !i.alive;
}
 
list_of_items.RemoveAll(ItemIsDead);

Because, as stated, C# has the runtime handle the memory management, once you've removed all references to the object it's out of your control.

well i need to change the structure of a few classes so the collection is irrelevant i might change it if it would allow to do so

i cant use "collection.remove" because i meant i just pass the collection element to other classes not the whole collection

and ofc no, im not talking about memory

You'll have to explain a bit more. Is the tree object changing state from being a growing tree to being on fire to being burned down? Or are you talking about the interface creating a list of valid targets and then not needing them after a target is selected? Or something else entirely?

its something much easier

i have a classe Tree

then i make with it my collection of game objects

List<Tree> trees;

then i pass my collection to other classes/methods, and they use it and modify it

lets say i have a sub collection

listotargettabletrees from which i take a single Tree mytargettree

then i pass mytargettree to the spell class that burn it

eventually the spell dmg destroy mytargetree

so i want the spell class that is only in possess of mytargettree to destroy it, ofc in my main game object lis trees

As long as you are removing the reference from the collections when it no longer needs to be there, then when the final reference no longer exists the garbage collector will take care of reclaiming the memory. So within your spell class if you have a property called "MyTargetTree" you simply need to set it to null to "delete" it:


mySpell.MyTargetTree = null;
// garbage collection will take care of reclaiming memory at some point in the future

If however you are referencing unmanaged resources within your MyTargetTree object (e.g. a D3D texture etc) then you will also want to use the IDisposable pattern. And instead use the using (var myTargetTree = ...) { ... } block or ensure you call MyTargetTree.Dispose().

Justin Stenning | Blog | Book - Direct3D Rendering Cookbook (using C# and SharpDX)

Projects: Direct3D Hook, EasyHook, Shared Memory (IPC), SharpDisasm (x86/64 disassembler in C#)

@spazzarama

 

Simple answer, you can't. Think of all your objects as "pointers" to the real object.

Even if you set the passed instance to null in your Spell class, the container still has a pointer to the object. And as long there's any pointer to an object somewhere it won't be removed. You need at least a way to notify your collection to remove that object.

One possibility, add a bool flag to your objects which is set to true if the object is to be removed. Set it to true by the Spell class. In your main loop you surely have a loop enumerating over the container. In there you can check that flag and remove all flagged objects.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Depends a lot on your architecture. For instance if the trees get iterated over every loop you could have an "alive" bool on them or something and have the list remove any trees that are set to dead. Using that method you could have the tree be in control of burning itself perhaps, i.e.: tree.CauseDamage(30); then CauseDamage will set the tree to dead if it runs out of health.

Another method if you wanted the tree to be removed right away is that you would have to have some class that controls the container of trees, for instance maybe you have a world object and the trees belong to it. In that case you could pass the tree to your spell method, have the spell check the tree's health and if it will kill it, you could tell the world object to remove the tree, referencing it either by an id or the reference to it you have. Keep in mind with this method the spell has to have some reference to the world, either already, it can get it from the tree, or it is passed as a parameter along with the tree.

You can do variants of that but those two methods are the main ones, either the trees have to somehow flag that they need to be removed or you have to physically "tell" the container that it needs to remove one, one way or another.

There are several things you need to be concerned about, and some things you don't.

You need to know and understand the lifetime of your objects. At some point they are created, they go through a process to become entities in your game that are (in this case) shown on the screen. Eventually they are removed from the screen, they stop affecting other entities in the game, they get removed from the game processing, the resources discarded and the memory reclaimed.

In C# you still need do all the steps in an object's life cycle except the last. You create the objects, load them up with resources, and process them. Eventually you are done processing them and you release the resources. All of these things you must be concerned with, and you need to plan and implement them.

In C# there is no explicit destruction of the object, you simply remove all references and the garbage collector eventually recycles the memory. Be sure you have disconnected it from everything, otherwise it won't be recycled.

This topic is closed to new replies.

Advertisement