Re: C# equivalent to a smart pointer???



"Ian" <Ian00Bell@xxxxxxxxx> wrote in message
news:vsPVg.40922$LH6.1135420@xxxxxxxxxxxxxxxxxxxxxxx
[...]
My understanding is that the use of pointers in C# is strongly
discouraged. The question then is there any other way for C# to implement
data sharing functionality similar to that provided by smart pointers in
C++?

I can't say that I'm completely clear on your particular use of the phrase
"smart pointer". However, in my experience this usually just means a
ref-counted pointer, in the manner used by COM. There is even an ATL type,
the CComPtr, that is commonly used to wrap the COM AddRef/Release
functionality.

If I understand your question correctly, this is essentially what you are
looking for. And if that's the case, then C# provides this functionality
innately through the reference type. For example:

class I1
{
}

class M1
{
public I1 _i1;
}

class M2
{
public I1 _i1;
}

void example()
{
M1 m1 = new M1();
M2 m2 = new M2();
I1 i1 = new I1(); // first reference to the I1 instance

m1._i1 = i1; // second reference to the I1 instance
m2._i1 = i1; // a third reference to the same I1 instance
m1._i1 = null; // now there's only two references again
m2._i1 = null;

// now there's only one reference to the I1 instance, the
// the local variable. Once we return out of this function,
// there will be no references and the I1 object itself will
// eventually be disposed of/freed/deleted/whatever through
// garbage collection
}

If you need for the object to be freed on a more timely basis than the
garbage collector will do it, then there are ways to explicitly dispose the
object. This is usually only necessary when the object itself maintains
some kind of reference to an unmanaged object (which usually actually means
a reference to some lower-level OS object that is in short supply). See
"using" and "Dispose" for more details.

As you can see from the above, unless you have a need to retrieve your "I"
objects later, you don't even need to maintain a global list of them. You
can create one, add it to all the "P" objects that need to process it, and
when they are all done, the "I" object will go away.

If, for some reason, you need to manipulate the "I" objects at some later
time, or need a way of knowing when an "I" object has been processed by all
of the "P" objects, you would need to maintain an extra reference somewhere
(perhaps in a List<T> object), and/or (depending on the exact need) add some
kind of "processing owner" count independent of the total references that
when is reduced back to zero (by the last processing thread that handles the
object) whatever processing needed at the end is done.

Whether this final action on the object can be done while disposing the
object, or you need the extra "processing monitoring" functionality added, I
can't say. Your problem description is too vague for us to know. (As far
as I know, it's not a good idea to do anything particularly expensive while
disposing an object...but if any final processing just involves minimal,
cleanup-like behavior you could just handle it while disposing). But
hopefully the above gives you a starting point, and you can figure out for
yourself what the correct approach is.

Pete


.



Relevant Pages

  • Re: Real World Flunks (Re: Why is Object Oriented so successful)
    ... AndyW wrote: ... "modeling the real world". ... CADD screen closely matches the object pointers in a typical real- ... I think the fact one has a reference to it ...
    (comp.object)
  • Re: The Java no pointer big fat lie!
    ... > reference types, it wouldn't be a good comparison. ... and what you can't - and there is a big difference in Java to C and C++. ... You cannot change it in the language itself, ... Thus I would distinguish pointers from ...
    (comp.lang.java.programmer)
  • Re: Array comparison
    ... > Deep and recursive. ... the pointers and comparing their referents instead of comparing the ... >> arrays, then I think you also need to argue against assigning arrays. ... semantics (notably, reference counting). ...
    (alt.comp.lang.borland-delphi)
  • Re: Pointers vs References: A Question on Style
    ... >> One good style rule is to never return null, ... Prefer references to pointers unless you need pointers' special ... The edit fields link to the XML via MVC. ... > 'funk', you should pass by reference, and catch errors at compile ...
    (comp.lang.cpp)
  • Re: couple of general questions
    ... > reference thing.I thought that pointers are under the reference ... the same value as the identifier of the caller. ... compiled code stores the value 20 directly into the variable that the ...
    (alt.comp.lang.learn.c-cpp)

Loading