- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Not conclusively: The new instance takes "this" as a parameter, and it is possible that is code not shown there is "ownership and lifetime management".
IF this is the case then deleting the object within this method may result in double deletion.
Be careful about assumptions.
Admin
I've never seen so much unreachable code in one place in my life.
Admin
The DeleteIfNotNull is also useless. The specification for delete (since C++98) states, that deleting a nullpointer is allowed and does nothing.
Admin
For reference, the magic number is
RPC_E_FAULT
, meaning "RPC could not call the server or could not return the results of calling the server."See https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/705fb797-2175-4a90-b5a3-3918024b10b8
Observation: the if() doesn't mean that. It means "if the
HRESULT
returned fromRetrieveDetailsUsingOutlook97()
was one of the two gibivalues that indicate a failure". It's still WTF to call the function again with identical parameters, mind you, but the editing of the submission is also somewhat WTF.For reference, an
HRESULT
is a 32-bit unsigned integer type returned mostly from COM API calls and from calls to COM objects. It is defined so that if the high bit is 0, the return value indicates success, and if the bit is 1, the return value indicates failure. There aren't many named success values, andS_OK
is probably the most commonly-used one (value0
. But there are others, and the one that is most guilty of WTF-ness isS_FALSE
, whose value is1
!But in the 2020s, if someone is still using HRESULTs (or, worse, COM objects), that person is TRTRTRWTF.
Admin
Maybe the FAILED macro returns true if there was no failure
Admin
More importantly, "p_cio_Local" is never used in any way, so why was it created?
Admin
An enforceable code review policy first requires that people care.
Many devs just don't bother reviewing other devs' code.
Among those who do review, sometimes they only have a very superficial look, and when you give them a commit of 20000 lines with deep changes to the code and to the database, they add couple of useless comments "You put 2 spaces between that bracket and that curly brace", rather than looking for the really important stuffs like whether there's proper access control, whether the code properly handles errors, whether the database is properly structured, whether the logic flows well, etc.
So, having a good code review is damn valuable, but also rare.
Also, management is super happy to mention the code reviews as part of the quality of the process when they're audited, but in the actual day-to-day work, they want to rush the shipping of the releases by every possible mean, and as a result the review isn't done as properly as it should.
Admin
... and this is why we have smart pointers (with destructors to clean up memory), to save us from dumb programmers.
Admin
C++'s essential defect is that it makes doing the right thing with memory easy, but it does not make doing the wrong thing hard.
Admin
The first function might not be so bad if there is some implied ownership or other specialized memory management involved in passing
this
to the constructor of the new object. But the fact that your first instinct is to suspect a typical memory leak mistake rather than understanding that specialized ownership is a problem.Admin
Decades of programming have taught me that there is nothing that will save us from dumb programmers. As the saying goes, make something idiot proof and nature makes a better idiot. This applies to programming.
Admin
But there isn't any sort of implied ownership- we
new
an object, and let the pointer go out of scope beforedelete
ing it. Could they have overriddennew
in some fashion? Maybe changing their whole program to use Arena Allocation or something? Sure, it's possible. But did they? Almost certainly not.Admin
Excessive use of specialised ownership is, indeed, a problem, because it isn't visible at the point of use.
In fact, I'm tempted to suspect two things:
-Werror
or equivalent, or, more likely, they have deactivated this and probably many other warnings.Admin
So we're writing COM code, which is why you're seeing the double pointer. This is not a WTF. The logic is :-)
The SUCCEEDED(hr) and FAILED(hr) macros are the proper way to handle success and failure in COM. Every COM error is negative and every success positive, so these are quite small macros :-)
The magic number is RPC_E_FAULT. With the right include, the symbol exists. This is an RPC lib error ("could not call server or return results from call")..
Admin
Remy is right, that function is exactly as it appears in the code base. There is no implied ownership anywhere. WYSIWYG
Admin
As Guy Steele's lambda papers explain, contorting your code to have "one point of return" is futile, because the only (normal) return point is whatever the caller pushed on the stack.
Admin
"Then we try and do an operations, and if FAILED is the result, we… return the result. We don't return the result if it succeeded. " Sorry, Remy but here you are wrong. Code is returning HRESULT that contains error code. There is going to be return (it is after all mandatory), so that return will, well, return result code in hr.
Basic WinAPI/COM coding.
Admin
Beside personal hatred, why would you say it about person writing COM code or dealt with HRESULT. WinAPI is still very alive and not only there are plenty of COM objects in Windows, but there are newer versions of it too. (Like WinRT)
Admin
The mix of coding styles is another code smell. And who give variables names like
p_cio_Folder
,p_mrl_To
etc. ? It's neither camelCase, nor PascalCase, nor snake_case.Admin
Ignoring HRESULTs that are not FAILED is not necessarily a code smell. Many COM functions can only return either S_OK or an error code. So if it is not FAILED, it was S_OK and you can happily continue torturing MS Outlook via a COM interface until you get the next error - or if you got none, return S_OK.
Admin
While the DeleteIfNotNULL( p_cio_Local ); is never called it is itself also code smell. Deleting null pointer is always valid, well defined in standard. The code should use std::unique_ptr or something similar. On never use new at all as it is stupid for local variables which have well defined lifetime ( if the object is very big then mayby heap variable is ok).
Admin
We do not know the type of uiMessageType and as such we do not know that == and != are the opposite. The logic implemented may make sense. I doubt it, but it just may make sense.
Even if == and != are each others logic opposites, we dont know if == changes state of uiMessageType so the following != may be needed.
Admin
Nobody seems to have noticed that the
else
branch in the second example also retestsuiMessageType