Re: private construction on GCC
brianhray@gmail.com wrote:
void Release() {
if (count > 0) count--; //< Exception Here
if (count <= 0) {
delete this;
}
}
Your function here checks for error conditions without handling them.
If the count is non-positive on entry into the function, then your
program contains a software defect.
The line
if (count > 0) count--;
essentially says:
if (the refcount has not been mismanaged)
decrement it
What's the point of testing for an error condition which you don't
intend to handle, or at least report in an error log?
The next statement is even worse:
if (count <= 0) {
delete this;
}
In other words:
if (the refcount is now zero, or has been mismanaged)
blow away the object;
You have ensured that if there is a refcount mismanagement problem in
the program, it will not only be silently ignored, it will actually be
compounded by attempts to free the object two or more times.
How would the refcount ever drop below zero, unless the object were
corrupt? It's only manipulated by the object construction and by Grab()
and Release(). Release() ensures that the count cannot drop below zero,
so why would you check using <= 0?
// attribute functions
long Count() const { return count; }
Nothing in the program should care about the absolute reference count,
so the presence of a public function which returns it is suspicious.