Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Double pointer in C# / .NET

Back to Forum: Tech Off
  • User profile image
    object88

    I have a service object that I want to pass around to a bunch of child objects.  This service may go dead at various points, so I'll need to replace it from time to time.

    In C++, I could have kept a double pointer around, and when the object went belly up, I would just have to fix the original pointer.

    Is this possible in C#?  I would have thought I could have used "ref", but that seems to only apply to parameters, not fields.

    Or do I need to suck it up and either create a wrapper object that maintains the service reference, or just update all child objects when the service goes dead?

    C++ example:

    class CMainObject
    {
       CFoo *pFoo;

       void ReplaceFoo(CFoo *pNewFoo)
       {
          this->pFoo = pNewFoo;
       }

       void CreateChildObject()
       {
          CSubObject *pSubObject = new CSubObject(&pFoo);
       }
    }

    class CSubObject
    {
       CFoo **ppFoo;

       CSubFoo(CFoo **ppFoo)
       {
          this->ppFoo = ppFoo;
       }

       void DoSomething()
       {
          *ppFoo->DoSomethingInteresting();
          *ppFoo->DoSomethingBoring();
       }
    }

    (I'm sure I screwed something up in the example above; it's been a good long while since I've used C++.)

  • User profile image
    Sk4rlath

    Making that wrapper object looks like the only way I can see to do what you want - and I think it's probably smarter.

    Though the double pointer works, if this service can indeed die at *any* time, that makes the system a little fragile.  What happens if the service dies mid-user method?  Is your service object prepared to handle that?  What about threading - isn't there a race condition in replacing the pointer?

    Actually, shouldn't you be able to do the service replacement within the service object?  Why do you need to replace the actual instance?  Can't you just do some finagleing about with internal state?

  • User profile image
    object88

    Good questions, lemme throw in some more background.

    I'm working on a WinForms user control, and the "server" object is a 3rd party wrapper for a WebService object.  At some point, the server may timeout, and any further calls on the same server object will fail.

    As for race conditions, that is a danger, but I'm combatting that by wrapping calls to the server object in locks on the server object itself.  I'm only ever making one call to the server at a time (typically to get a bit of binary data), and I don't have to send any updates to the server, so I don't have to worry about inconsistent state.

    It's outside the realm of my control to replace the server object.  I *may* be able to repair the object, but if the server requires a new login, I don't have the user's password.  So it would be good if I simply responsibly reported a failure and let the application handle the cleanup.

    Yesterday, I implemented a simple wrapper object for the server object to share it as necessary.  I think this is the solution I'll stick with.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.