Jump to content

  • Log In with Google      Sign In   
  • Create Account


Is it acceptable to call a destructor?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
40 replies to this topic

#21 Bregma   Crossbones+   -  Reputation: 4975

Like
3Likes
Like

Posted 19 October 2012 - 12:32 PM

I would argue Brother Bob's way is most efficient and clean, and has the most elegant and clean syntax. Then again, I'm used to reading C++.

Laerning how to use the standard library (std::remove_if(), std::vertex()) and the language features (lambdas, auto type deduction) is the boss fight that will unlock the next level.
Stephen M. Webb
Professional Free Software Developer

Sponsor:

#22 BeerNutts   Crossbones+   -  Reputation: 2793

Like
2Likes
Like

Posted 19 October 2012 - 01:00 PM

I would argue Brother Bob's way is most efficient and clean, and has the most elegant and clean syntax. Then again, I'm used to reading C++.

Laerning how to use the standard library (std::remove_if(), std::vertex()) and the language features (lambdas, auto type deduction) is the boss fight that will unlock the next level.


The problem is this is a beginner's forum, and when you start putting lambda functions, using std::bind, functors, or any other somewhat advanced techniques, it's easy to get discouraged. As you said, you're used to looking at C++ (and, probably used to looking at C++11).

Do you really expect a beginner, who is just learning what a for loop does, to understand a lambda function?

So, IMO, this forum should stick to fundamentals and being obvious about how to conquer problems, and this would exclude any C++11.

The boss fight is for the experienced players, not someone who just picked up the joystick for the first time.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#23 Katie   Members   -  Reputation: 1312

Like
2Likes
Like

Posted 19 October 2012 - 01:12 PM

At the level you're working at, you do not call destructors.

Normally people do not call the destructors. Since C++ is clearly pretty new to you, take the advice and do what everyone else does and don't call the destructors directly. Because if you do, you'll be back here in a week asking people to debug the weird crashes in your program and that road is going to be really hard work because suddenly you're in a place where very few people will be able to help you because everyone else doesn't call the destructors because they don't want to be in that place.

There is a reason people learning to juggle start with bean bags and not running chainsaws. It's less exciting, that's for sure. But it's also less exciting.

#24 Brother Bob   Moderators   -  Reputation: 8011

Like
0Likes
Like

Posted 19 October 2012 - 01:33 PM


I would argue Brother Bob's way is most efficient and clean, and has the most elegant and clean syntax. Then again, I'm used to reading C++.

Laerning how to use the standard library (std::remove_if(), std::vertex()) and the language features (lambdas, auto type deduction) is the boss fight that will unlock the next level.


The problem is this is a beginner's forum, and when you start putting lambda functions, using std::bind, functors, or any other somewhat advanced techniques, it's easy to get discouraged. As you said, you're used to looking at C++ (and, probably used to looking at C++11).

Do you really expect a beginner, who is just learning what a for loop does, to understand a lambda function?

So, IMO, this forum should stick to fundamentals and being obvious about how to conquer problems, and this would exclude any C++11.

The boss fight is for the experienced players, not someone who just picked up the joystick for the first time.

Fundamental does not equate simple. Everything in every language, literally, is just syntactic sugar on top of machine code that adds a levels of abstraction. The more of this sugar you exclude, the more of other things you have to learn instead. I don't think manually erasing elements is any more obvious than using functions that the standard library already provides.

I have already responded to an earlier poster that I agree that my solution as a whole may be too much for a beginner (and frankly, I only posted it because alvaro asked for it). But I also argued that, once you have accepted the solution in general, the C++11 features I included actually improves the simplicity of it.

Ignoring the C++11 features because they add complexity is wrong in my opinion. You can ignore my solution as a whole, but it would be outright stupid to remove the C++11 from it because that's going to turn it into a mess (or an even greater mess, if you like).

#25 Telastyn   Crossbones+   -  Reputation: 3726

Like
1Likes
Like

Posted 19 October 2012 - 01:36 PM

So, IMO, this forum should stick to fundamentals and being obvious about how to conquer problems, and this would exclude any C++11.


I love the irony.

This sort of thing isn't even a problem in the languages we regularly recommend to beginners. Maybe we should focus on excluding C++ altogether?

If we're going to include C++, then include it properly.

Edited by Telastyn, 19 October 2012 - 01:37 PM.


#26 AlexB.hpp   Members   -  Reputation: 201

Like
0Likes
Like

Posted 19 October 2012 - 02:47 PM

Are you saying I put it a step further, lol?

I'm unfamiliar with the syntaxes some of you are giving me, or I don't really find them very clean/readable (no offense). So to me it looks like I'm doing less work or I'm lacking in some area of efficiency or functionality. I kind of think that way whenever I run into an unfamiliar syntax or code that looks intimidating to read through.

Compared to the methods that have been discussed in the thread compared to my posted way of doing it, which is the best in terms of efficiency and cleanliness? In C++ or something similar, please. Or even pseudocode!

You can make a decision on basic points: how much objects you want to use, what features you want to have and so on It's another great lesson what you have to learn on programming. But I suggest you to do it as good as possible, to use it in the next time ;)

ADDED

After reading this post by Brother Bob, I wanna add that you extremely have to learn basics of working with memory. It will strongly help you in the future. As middle point I want you to get some examples of SmartPointers(WeakPtr, StrongPtr, AutoPtr and so on).

PS when you will get a point of code from Brother Bob and tell how to use it in real application, then they will call you advantaged programmer ;)

Edited by AlexB.hpp, 19 October 2012 - 03:15 PM.

C x 2 C x o C x C f nice C x C s C x C c

#27 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 20 October 2012 - 12:11 PM

I'm not just learning about how to use for loops, lol. If you want to explain lambda functions to me, then I would gladly accept it.I've been meaning to anyway.
The reason I'm asking about this topic is that I'm making a game, and I want to make it as streamlined and advanced as I can with the tools given to me.
Most of the time when I ask questions here it has to do with conventions. Like, is it accepted by the general programming community to do this or that, etc. and this is one such question. But thank you all for the information once more.

Edited by StoneMask, 20 October 2012 - 12:14 PM.


#28 superman3275   Crossbones+   -  Reputation: 2011

Like
0Likes
Like

Posted 20 October 2012 - 01:38 PM

You could make your own Delete() method, and that would set a boolean flag in the object (IsNull, for example) to true and delete any memory you may have allocated. Then, you could have a get() function (Or integrate this into the class with the boolean flag) to make sure you never tried to access members of the class that weren't there anymore.

I'm a game programmer and computer science ninja ph34r.png!

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

 

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here smile.png!


#29 Brother Bob   Moderators   -  Reputation: 8011

Like
2Likes
Like

Posted 20 October 2012 - 01:54 PM

You could make your own Delete() method, and that would set a boolean flag in the object (IsNull, for example) to true and delete any memory you may have allocated. Then, you could have a get() function (Or integrate this into the class with the boolean flag) to make sure you never tried to access members of the class that weren't there anymore.

That is entirely the wrong approach. If you delete an object and access it later, then the object shouldn't have been deleted in the first place because it was still in use. You have a lifetime management issue in that case and the proper way to handle that is to ensure that objects that are in use are never deleted, not to delete them arbitrarily and add a safety net just in case.

#30 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 20 October 2012 - 02:20 PM

The way I handle it is a boolean that determines whether or not the object "exists". If it doesn't exist, I make its position far from anything that it could interact with, I disable its actions, and erase it from the board. Then once your character is out of the environment that has the enemy objects, they stay in a state of nonexistence until the next time they need to be initialized. So I would delete them right before I make more of them, and if the program ends, I delete them regardless.

#31 superman3275   Crossbones+   -  Reputation: 2011

Like
0Likes
Like

Posted 20 October 2012 - 02:45 PM

Wait, you move them "very far away". That is some really bad resource management. What you should do is stop drawing it altogether and delete all of it's member variables. I know there's some way to dot his without using new and delete, however I can't put my finger on it.

I'm a game programmer and computer science ninja ph34r.png!

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

 

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here smile.png!


#32 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 20 October 2012 - 02:57 PM

I do stop drawing them altogether. I make it so the program doesn't access them anymore. The objects still exist, but nothing about them is accessed.

#33 superman3275   Crossbones+   -  Reputation: 2011

Like
0Likes
Like

Posted 20 October 2012 - 03:18 PM

Yes, but it doesn't matter if you don't draw them or access them, you're still using the R.A.M. required for them. Resource Management, although in this case not vital (Because it's a small game) is very important. What's the point in having them there if you're not going to use them? There isn't one. You need to delete the memory to free up space for other programs / more variables / bigger programs.

I'm a game programmer and computer science ninja ph34r.png!

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

 

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here smile.png!


#34 yckx   Prime Members   -  Reputation: 1163

Like
1Likes
Like

Posted 21 October 2012 - 03:40 PM

Depending on the game, it may actually be more efficient to set a kill flag and ignore dead objects than to frequently new/delete objects.

#35 kunos   Crossbones+   -  Reputation: 2205

Like
0Likes
Like

Posted 22 October 2012 - 12:01 AM

Depending on the game, it may actually be more efficient to set a kill flag and ignore dead objects than to frequently new/delete objects.


true, but that should be an optimization that comes AFTER the new/delete has been proven as the application bottleneck. An application should use the right data structure for the problem that is trying to solve, and then, only if necessary, implement an optimized version hiding that optimization away from the rest of the application.

The possibility that a game will need frequent new/delete is very low.. it usually only applies to particles and bullets (which are usually designed with memory pools straight away for this very reason) .

99% of the time, this is a premature optimization that will only bite you back later in the dev process by making code more complex to reason about ( I always tell to the guys working with me, your code makes sense to you NOW, how much sense will it make in 2 months?) and, using dead flags, hide poor data handling until it's too late to fix it.
Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#36 AlexB.hpp   Members   -  Reputation: 201

Like
0Likes
Like

Posted 22 October 2012 - 12:50 AM

...
99% of the time, this is a premature optimization that will only bite you back later in the dev process by making code more complex to reason about ( I always tell to the guys working with me, your code makes sense to you NOW, how much sense will it make in 2 months?) and, using dead flags, hide poor data handling until it's too late to fix it.


That's why every project should have documentation: generated from code manuals, design doc, defined code convention, architecture doc and few other. This is programming too.
C x 2 C x o C x C f nice C x C s C x C c

#37 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 22 October 2012 - 01:23 AM

The enemies only have a coordinate position, an unsigned short timer variable, a facing direction, and a bunch of functions that can move it around using random number generation. There is no graphics handling to take care of with these objects. You can think of them as practically invisible.

So are you saying that deleting these objects would be better for every time they intersect with my projectile? Or should I check out my CPU Usage every time they're deleted, and compare that against just shoving them off to the side until later, or what?

What do you think of a similar way of handling the player's projectile object? My player object has a projectile object that merely pops out of existence at the end of its run, and the player always has exactly one.

Edited by StoneMask, 22 October 2012 - 01:45 AM.


#38 kunos   Crossbones+   -  Reputation: 2205

Like
1Likes
Like

Posted 22 October 2012 - 01:48 AM

So are you saying that deleting these objects would be better for every time they intersect with my projectile? Or should I check out my CPU Usage every time they're deleted, and compare that against just shoving them off to the side until later, or what?


i think you should do the thing that is mapping better to the world you are trying to simulate and that results in the cleanest and (possibly) shortest code.

This is simply a vector of Stuff* or, shared_ptr<Stuff> ... add and remove objects in a standard C++ way. Because when things die, they are not part of the "living things set" (your vector) and they are destroyed.. dead people are gone, they don't wear a hat saying "I'm dead" ;)
Get the game to a prototype stage and then evaluate what parts of the code need special attention.
And, honestly, since I don't think you're working on the new Crysis, it's VERY unlikely you'll find any bottleneck in the C++ standard library.
Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#39 Akashi   Members   -  Reputation: 268

Like
0Likes
Like

Posted 22 October 2012 - 10:49 PM

Ah, I hadn't considered a vector. Now that I look through the thread again they were mentioned once or twice.

Also, it's a small game, but I want to learn more about structure and efficiency. Even if it's small, I want it to be freakishly efficient.

#40 Álvaro   Crossbones+   -  Reputation: 12890

Like
2Likes
Like

Posted 23 October 2012 - 07:29 AM

Even if it's small, I want it to be freakishly efficient.


If you are obsessive about this kind of thing, I suggest you try to fight the impulse. If you can't, your psychiatrist might have some pills for you.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS