I convert a C # project to C ++ and ask a question about deleting objects after use. In C #, GC, of course, takes care of deleting objects, but in C ++ it must be done explicitly using the keyword delete.
delete
My question is, is it okay to follow every use of an object in the whole method and then delete it as soon as it goes out of scope (i.e. end / reassignment of the method)?
I know, however, that the GC waits for a certain size of garbage (~ 1 MB) before deleting; does this do this because there is overhead when using delete?
Since this is the game I am creating, there will potentially be many objects that will be created and deleted every second, so would it be better to keep track of pointers that are out of scope and after that size reaches 1 MB and then delete the pointers?
(as a side note: later, when the game is optimized, objects will be loaded once at startup, so there is not much luck during the game process)
Your problem is that you are using C ++ pointers.
, , . , , . - ( CC BY, ).
. , , , - : . , : delete.
. , GC , (shared_ptr) . ( !) , small object allocator.
shared_ptr
RAII ++, delete .RAII , , , , - . .
# ++ . .NET GC ++. .NET ( ), - . ++ , , . , , "" . .NET , GC . ++ .
.NET ++. , .NET , GC. GC . , ++ , . , , , . ++, RAII .
, " ++" " ++".
, ++. , Boehm . , ( , #, , .)
, ++, . #, ++ , , , , . ++ , ; a Monster - , , , . , . #, , , dispose - , , , , , Observer, , . ++ , , dispose, delete object.
Monster
dispose
shared_ptr , #, , , .
, - - , inline/ . , , - .
, delete , . , delete , . .NET .
A design that basically corresponds to what is in C ++ tr1::shared_ptr<>, which stores the control counter of the object and is freed when it drops to zero. The first approach to doing the work would be to make all C # references to C ++ tr1::shared_ptr<>. Then you can go to those places where this is a performance bottleneck (only after you have verified with the profile that this is the actual bottleneck) and move on to more efficient memory handling.
tr1::shared_ptr<>
The GC C ++ function has been much discussed in SO.
Try to read it!
Garbage Collection in C ++