pplux/px

Remove unnecessary resetting of class attributes in destructors

elfring opened this issue · 4 comments

pplux commented

Hi,

I'm sorry but I have to disagree with your opinion, and in fact with the stack overflow post you mention, there is a reason to set it to null. Let me explain why, I'll try to do my best.

Imagine that you have an Instance of an Object A, in the heap, and you pass the pointer to other part of your code and then delete the object. Of course, that might explode in that moment, or fail to do so, the thing is if from that other part you try to access A, and the pointer it contains look as valid... there is no way to detect the Instance has been deleted.

Setting that pointer to null helps a bit in case of future debugging, in this case accessing the pointer will be invalid at the same moment you try to do so... and that narrows the problem a bit, failing to set that to null might lead to erratic behaviour but not necessarily a crash.

I have suffered such crashes, and is a bless to have some code that crashes near the problem, and not later randomly far away of the real problem.

Setting that pointer to null helps a bit in case of future debugging,

It can be unsure if this modfification will be really helpful finally.

How do you think about to improve the system configuration possibilities also for this software implementation?

in this case accessing the pointer will be invalid at the same moment you try to do so...

The corresponding object became unusable directly after execution of the delete statement.

and that narrows the problem a bit,

This depends on some circumstances.

failing to set that to null might lead to erratic behaviour

There are software failures to consider generally if object lifetimes are improperly taken into account.

but not necessarily a crash.

pplux commented

How do you think C++ actually prevents you from using an deleted object? The answer is simple, it does not, setting those pointers to null helps a bit when debugging if you accidentally access an invalid object given its position in memory. That should not happen in the first place but... it doesn't hurt either.

Thanks for taking your time to address the issue, but I made my mind about this sort of thins long time ago not for this project in particular.

How do you think C++ actually prevents you from using an deleted object? The answer is simple, it does not,

Not directly …

setting those pointers to null helps a bit when debugging if you accidentally access an invalid object given its position in memory.

But you could try harder to reduce the probability for wrong data accesses by the usual means for safer software design also with help from this programming language.

That should not happen in the first place

Agreed.

but... it doesn't hurt either.

Extra source code can influence software behaviour and its maintainability in undesirable ways, can't it?