Click here to Skip to main content
12,452,066 members (25,008 online)

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Something original Pin
BCantor13-Aug-13 5:05
memberBCantor13-Aug-13 5:05 
GeneralRe: Something original Pin
Jacek Gajek13-Aug-13 5:51
memberJacek Gajek13-Aug-13 5:51 
GeneralRe: Something original Pin
Brisingr Aerowing19-Aug-13 16:20
professionalBrisingr Aerowing19-Aug-13 16:20 
GeneralRe: Something original Pin
ExcellentOrg21-Aug-13 0:04
groupExcellentOrg21-Aug-13 0:04 
RantFsking ASP.NET! Pin
Richard Deeming6-Aug-13 10:00
professionalRichard Deeming6-Aug-13 10:00 
GeneralRe: Fsking ASP.NET! Pin
Bernhard Hiller6-Aug-13 20:57
professionalBernhard Hiller6-Aug-13 20:57 
GeneralRe: Fsking ASP.NET! Pin
Sentenryu7-Aug-13 9:08
memberSentenryu7-Aug-13 9:08 
GeneralWhen things to more than you think Pin
imagiro6-Aug-13 7:17
memberimagiro6-Aug-13 7:17 
I just spent nearly two days debugging an COM exe server using ATL and boost. There is a boost thread, acting as an asynchronous task manager that gets task queued which do callbacks into COM objects (into JScript objects).

The thread was hanging in join(), even though a breakpoint set at the end of the thread function was hit.

The code was written by a collegue, who is a god with boost, but rather new to ATL - and he hates it. Me in turn did not use a lot of boost (yet), but with COM/ATL I have quite some experience, and I like it. So first I suspected boost to be somehow buggy, although I could not really imagine that such a widely used thing like boost::thread would have such a serious bug or a somehow screwed functionality.

Here is (shortened) how the thread looks:
void operator()()
{
  CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  BOOST_SCOPE_EXIT_ALL(&) { CoUninitialize(); };
  try {
    // lots of code here...
    // e.g. running a task from a task queue calling back into javascript
    // and creating a few js-objects...
    // and more...
    // and more...
  } catch (boost::thread_interrupted &) {
    ATLTRACE(L"Thread interrupted\n");
  }
}
As I said: A breakpoint set at the last closing curly brace got hit, but the join() call from the main thread that triggered the thread interruption never finished.

After a while (quite a while!) of playing around, debugging (a release version btw, since the problem happened only in a release build) and staring at the code I noticed the neat little line near the top of the thread:
BOOST_SCOPE_EXIT_ALL(&) { CoUninitialize(); };
"Wait a sec! What's that?"

"BOOST_SCOPE_EXIT_ALL - Maybe this means, it executes some code when the current scope exits? Maybe CoUninitialize()?. And doesn't CoUninitialize() block, when the current thread still holds some COM object references?"

So I started to dig into the code of the actual tasks that were executed here, and one of them in deed creates some objects. But since my colleague learned COM from me and I slap his fingers everytime I see a raw pointer somewhere, and also from what I found in the code there should not be a problem. The object was created nicely via
CComPtr<IDispatch> createJSObjectInstance()
{
  CComPtr<IDispatchEx> creatorObject = threadMarshaller->get();
  DISPPARAMS params = {0};
  _variant_t result;
  HRESULT hr = creatorObject->InvokeEx(DISPID_VALUE, LOCALE_USER_DEFAULT, DISPATCH_METHOD,
      &params, result.GetAddress(), NULL, NULL);
  // error handling omited
  return CComPtr<IDispatch>(static_cast<IDispatch*>(result));
}
"Safepointers everywhere, correct _variant_t handling, yada yada.. Looks good.."

Or not?

Well - looking at the implementation of _variant_t I found this:
// Extracts a VT_DISPATCH into an IDispatch*
//
inline _variant_t::operator IDispatch*() const
{
    if (V_VT(this) == VT_DISPATCH) {
        if (V_DISPATCH(this) != NULL) {
            V_DISPATCH(this)->AddRef();
        }
        return V_DISPATCH(this);
    }
 
    _variant_t varDest;
    varDest.ChangeType(VT_DISPATCH, this);
 
    if (V_DISPATCH(&varDest) != NULL) {
        V_DISPATCH(&varDest)->AddRef();
    }
 
    return V_DISPATCH(&varDest);
}
There are AddRef() calls! That would explain a lot!

And in deed: Changing the line
return CComPtr<IDispatch>(static_cast<IDispatch*>(result));
to
return CComPtr<IDispatch>(result.pdispVal);
fixed the problem.

My boss just said "Geez!"

Some coding guidelines forbid operator overloading. Google e.g. says: "Do not overload operators except in rare, special circumstances." Although they have other reasons I would say that such side effects is exactly what operators should not have. This is very bad design, and this one is clearly on MS.

However, my boss is smiling again and I had a more or less successful day.

Geez!
GeneralRe: When things to more than you think Pin
Super Lloyd6-Aug-13 21:13
memberSuper Lloyd6-Aug-13 21:13 
GeneralUndoubtedly this is my own ignorance in glorious action but... Pin
Andy Bantly5-Aug-13 7:52
memberAndy Bantly5-Aug-13 7:52 
GeneralRe: Undoubtedly this is my own ignorance in glorious action but... Pin
Matt U.5-Aug-13 8:10
memberMatt U.5-Aug-13 8:10 
GeneralRe: Undoubtedly this is my own ignorance in glorious action but... Pin
Andy Bantly5-Aug-13 9:22
memberAndy Bantly5-Aug-13 9:22 
GeneralRe: Undoubtedly this is my own ignorance in glorious action but... Pin
Matt U.5-Aug-13 10:02
memberMatt U.5-Aug-13 10:02 
GeneralRe: Undoubtedly this is my own ignorance in glorious action but... Pin
Andy Bantly6-Aug-13 10:54
memberAndy Bantly6-Aug-13 10:54 
GeneralSo you think you know what double.Parse does? Pin
harold aptroot2-Aug-13 23:27
memberharold aptroot2-Aug-13 23:27 
GeneralRe: So you think you know what double.Parse does? Pin
PIEBALDconsult3-Aug-13 5:31
professionalPIEBALDconsult3-Aug-13 5:31 
GeneralRe: So you think you know what double.Parse does? Pin
Grimlock4-Aug-13 8:25
professionalGrimlock4-Aug-13 8:25 
GeneralRe: So you think you know what double.Parse does? Pin
harold aptroot4-Aug-13 9:07
memberharold aptroot4-Aug-13 9:07 
GeneralRe: So you think you know what double.Parse does? Pin
Grimlock7-Aug-13 7:38
professionalGrimlock7-Aug-13 7:38 
GeneralRe: So you think you know what double.Parse does? Pin
harold aptroot7-Aug-13 7:51
memberharold aptroot7-Aug-13 7:51 
GeneralRe: So you think you know what double.Parse does? Pin
Rob Grainger15-Aug-13 0:18
memberRob Grainger15-Aug-13 0:18 
GeneralRe: So you think you know what double.Parse does? Pin
Amitosh S.M.4-Aug-13 20:50
memberAmitosh S.M.4-Aug-13 20:50 
GeneralRe: So you think you know what double.Parse does? Pin
Jacek Gajek6-Aug-13 10:56
memberJacek Gajek6-Aug-13 10:56 
GeneralRe: So you think you know what double.Parse does? Pin
harold aptroot6-Aug-13 10:59
memberharold aptroot6-Aug-13 10:59 
GeneralRe: So you think you know what double.Parse does? Pin
Jacek Gajek6-Aug-13 11:15
memberJacek Gajek6-Aug-13 11:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.8.160826.1 | Last Updated 28 Aug 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid