Yet Another Question About Smart Pointers (YAQAS)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johnny Hansen

    #1

    Yet Another Question About Smart Pointers (YAQAS)

    Hello,

    I've been trying to implement smart pointers in C++ (combined with a
    reference counter) because I want to do some memory management. My code
    is based on the gamedev enginuity articles, various books and ...
    whatever I could find on the subject :-)

    I'll leave out the reference counter part because its pretty basic, but
    my reference counter class is called xObject. My smart pointer class is
    called xPointer.

    My problem is that I can't figure out how to make my smart pointer
    object a pointer. Look at the following code:

    --------------- CUT
    void test()
    {
    //xShaderResource is derived from xObject
    class xPointer<xShade rResource> myShader2 = new xShaderResource ();
    myShader2->loadResource(N ULL); //testing
    }
    --------------- CUT

    This works like a charm. The xShaderResource object will be destroyed in
    my garbage collector because myShader2 is beeing destructed after it
    goes out of scope when test() returns.

    So far so good, but what if let's say I have a vector/list with 1000+
    xPointer objects (imagine a resource manager)? They will never go out of
    scope, right?

    What I need is to do something like this:

    --------------- CUT
    class xPointer<xShade rResource> *myShader2 = new
    xPointer<xShade rResource>(new xShaderResource ());
    myShader2->loadResource(N ULL);
    --------------- CUT

    This should enable me to run through the list and do something like this:

    delete (*iterator);

    on relevant objects thus forcing them to go away and get collcected. Can
    this be done? I kinda figure I need to change/add my operators
    somewhere, but I can't figure out how :-(

    Can anyone help?

    -------------------------------- Relevant code
    template<class X>
    class xPointer
    {
    protected:
    X* obj;
    public:

    xPointer()
    {
    obj = 0;
    }

    xPointer(X *oP)
    {
    obj = 0;
    *this = oP;
    }

    ~xPointer()
    {
    if( obj )
    obj->Release();
    }

    X& operator*(void) { return *obj; }

    void operator =(X *oP)
    {
    if( obj )
    obj->Release();
    obj = oP;
    if( obj )
    obj->Alloc();
    }

    void operator =(const xPointer<X> &oP)
    {
    if( obj )
    obj->Release();
    obj = oP.obj;
    if( obj )
    obj->Alloc();
    }

    X* operator ->(void)
    {
    return obj;
    }
    };
  • David Hilsee

    #2
    Re: Yet Another Question About Smart Pointers (YAQAS)

    "Johnny Hansen" <webmaster@suxx .dk> wrote in message
    news:cjv9b9$f9e $1@news.cyberci ty.dk...[color=blue]
    > Hello,
    >
    > I've been trying to implement smart pointers in C++ (combined with a
    > reference counter) because I want to do some memory management. My code
    > is based on the gamedev enginuity articles, various books and ...
    > whatever I could find on the subject :-)
    >
    > I'll leave out the reference counter part because its pretty basic, but
    > my reference counter class is called xObject. My smart pointer class is
    > called xPointer.
    >
    > My problem is that I can't figure out how to make my smart pointer
    > object a pointer. Look at the following code:
    >
    > --------------- CUT
    > void test()
    > {
    > //xShaderResource is derived from xObject
    > class xPointer<xShade rResource> myShader2 = new xShaderResource ();
    > myShader2->loadResource(N ULL); //testing
    > }
    > --------------- CUT
    >
    > This works like a charm. The xShaderResource object will be destroyed in
    > my garbage collector because myShader2 is beeing destructed after it
    > goes out of scope when test() returns.
    >
    > So far so good, but what if let's say I have a vector/list with 1000+
    > xPointer objects (imagine a resource manager)? They will never go out of
    > scope, right?
    >
    > What I need is to do something like this:
    >
    > --------------- CUT
    > class xPointer<xShade rResource> *myShader2 = new
    > xPointer<xShade rResource>(new xShaderResource ());
    > myShader2->loadResource(N ULL);
    > --------------- CUT
    >
    > This should enable me to run through the list and do something like this:
    >
    > delete (*iterator);
    >
    > on relevant objects thus forcing them to go away and get collcected. Can
    > this be done? I kinda figure I need to change/add my operators
    > somewhere, but I can't figure out how :-(
    >
    > Can anyone help?
    > [...][/color]

    Well, first consider using a reference-counted pointer library. If you want
    to write your own, then take a look at the FAQ's answers about reference
    counting (http://www.parashift.com/c++-faq-lite/ Section 16 items 21 "How do
    I do simple reference counting" on), because they contain useful snippets of
    code for implementing reference counting.

    Now, once you have a working reference-counted smart pointer stored in a
    std::list or std::vector, removing elements so that the managed
    reference-counted object may be deallocated is about as simple as removing
    any other element from the container. A simple example (using std::list)

    std::list<smart pointertype>::i terator it = /* something to remove */;
    myList.erase( it );

    will remove the smart pointer from the std::list, causing the smart
    pointer's destructor to be invoked, which will cause the managed object's
    reference count to be decreased by one (and deallocated, etc if
    appropriate).

    --
    David Hilsee


    Comment

    • John Harrison

      #3
      Re: Yet Another Question About Smart Pointers (YAQAS)


      "Johnny Hansen" <webmaster@suxx .dk> wrote in message
      news:cjv9b9$f9e $1@news.cyberci ty.dk...[color=blue]
      > Hello,
      >
      > I've been trying to implement smart pointers in C++ (combined with a
      > reference counter) because I want to do some memory management. My code is
      > based on the gamedev enginuity articles, various books and ... whatever I
      > could find on the subject :-)
      >
      > I'll leave out the reference counter part because its pretty basic, but my
      > reference counter class is called xObject. My smart pointer class is
      > called xPointer.
      >
      > My problem is that I can't figure out how to make my smart pointer object
      > a pointer. Look at the following code:
      >
      > --------------- CUT
      > void test()
      > {
      > //xShaderResource is derived from xObject
      > class xPointer<xShade rResource> myShader2 = new xShaderResource ();
      > myShader2->loadResource(N ULL); //testing
      > }
      > --------------- CUT
      >
      > This works like a charm. The xShaderResource object will be destroyed in
      > my garbage collector because myShader2 is beeing destructed after it goes
      > out of scope when test() returns.
      >
      > So far so good, but what if let's say I have a vector/list with 1000+
      > xPointer objects (imagine a resource manager)? They will never go out of
      > scope, right?[/color]

      Wrong. They will go out of scope when they are removed from the vector or
      list, or when the vector or list itself is destroyed.
      [color=blue]
      >
      > What I need is to do something like this:
      >
      > --------------- CUT
      > class xPointer<xShade rResource> *myShader2 = new
      > xPointer<xShade rResource>(new xShaderResource ());
      > myShader2->loadResource(N ULL);
      > --------------- CUT
      >
      > This should enable me to run through the list and do something like this:
      >
      > delete (*iterator);[/color]

      You are undoing the good work that smart pointers do. You are making life
      difficult for yourself by going back to raw pointers. Smart pointers are
      more powerful than you realise. You don't need to do any more work. You're
      done.

      john


      Comment

      • Johnny Hansen

        #4
        Re: Yet Another Question About Smart Pointers (YAQAS)

        John Harrison wrote:[color=blue]
        > You are undoing the good work that smart pointers do. You are making life
        > difficult for yourself by going back to raw pointers. Smart pointers are
        > more powerful than you realise. You don't need to do any more work. You're
        > done.[/color]

        I must be missing something, because I can't get it to work. Look at this:

        std::vector<xPo inter<xShaderRe source>*> myList;
        //defined in global scope for testing purposes
        //should be stored in my resource-manager object

        ------------------ CUT
        void test()
        {
        class xPointer<xShade rResource> myResource = new xShaderResource ();

        myList.push_bac k(&myResource) ;
        (*myList[0])->loadResource(N ULL);
        }
        ------------------ CUT

        When test() returns my "myResource " still gets destructed. I assume its
        because the myList only contains a pointer to the local global scope
        object, thus will contain a invalid pointer when test() returns. Not good.

        However trying to do:
        std::vector<xPo inter<xShaderRe source>> myList;

        fails miserably. How can I store the actual object in the list, not only
        the pointer? This is what I want (i think) :-)








        Comment

        • John Harrison

          #5
          Re: Yet Another Question About Smart Pointers (YAQAS)


          "Johnny Hansen" <webmaster@suxx .dk> wrote in message
          news:ck0dol$1dl 6$1@news.cyberc ity.dk...[color=blue]
          > John Harrison wrote:[color=green]
          > > You are undoing the good work that smart pointers do. You are making[/color][/color]
          life[color=blue][color=green]
          > > difficult for yourself by going back to raw pointers. Smart pointers are
          > > more powerful than you realise. You don't need to do any more work.[/color][/color]
          You're[color=blue][color=green]
          > > done.[/color]
          >
          > I must be missing something, because I can't get it to work. Look at this:
          >
          > std::vector<xPo inter<xShaderRe source>*> myList;[/color]

          Why use smart pointers if you then just go and use ordinary pointers on top?

          std::vector<xPo inter<xShaderRe source> > myList;

          Now all your problems go away.
          [color=blue]
          > //defined in global scope for testing purposes
          > //should be stored in my resource-manager object
          >
          > ------------------ CUT
          > void test()
          > {
          > class xPointer<xShade rResource> myResource = new xShaderResource ();
          >
          > myList.push_bac k(&myResource) ;[/color]

          myList.push_bac k(myResource);
          [color=blue]
          > (*myList[0])->loadResource(N ULL);[/color]

          myList[0]->loadResource(N ULL);
          [color=blue]
          > }
          > ------------------ CUT
          >
          > When test() returns my "myResource " still gets destructed. I assume its
          > because the myList only contains a pointer to the local global scope
          > object, thus will contain a invalid pointer when test() returns. Not good.
          >
          > However trying to do:
          > std::vector<xPo inter<xShaderRe source>> myList;
          >
          > fails miserably. How can I store the actual object in the list, not only
          > the pointer? This is what I want (i think) :-)
          >
          >[/color]

          If this fails miserably, it must be because your smart pointer is not coded
          correctly. How does it fail? Could you post the code.

          john


          Comment

          • John Harrison

            #6
            Re: Yet Another Question About Smart Pointers (YAQAS)

            >[color=blue]
            > If this fails miserably, it must be because your smart pointer is not[/color]
            coded[color=blue]
            > correctly. How does it fail? Could you post the code.
            >[/color]

            OK, looking at the code you posted earlier, the problem is that you have not
            defined a copy constructor for your smart pointer.

            Something like this is needed

            xPointer(const xPointer<X> &oP)
            {
            obj = oP.obj;
            if( obj )
            obj->Alloc();
            }

            john


            Comment

            • Johnny Hansen

              #7
              Re: Yet Another Question About Smart Pointers (YAQAS)

              John Harrison wrote:[color=blue][color=green]
              >>If this fails miserably, it must be because your smart pointer is not[/color]
              > coded[color=green]
              >>correctly. How does it fail? Could you post the code.[/color]
              > OK, looking at the code you posted earlier, the problem is that you have not
              > defined a copy constructor for your smart pointer.
              > Something like this is needed
              > xPointer(const xPointer<X> &oP)
              > {
              > obj = oP.obj;
              > if( obj )
              > obj->Alloc();
              > }[/color]

              Ahh, I've only done the =copy constructor. It works now! I owe you one,
              thanks alot :-)

              Comment

              Working...