Archived

This forum has been archived. Please start a new discussion on GitHub.

About the smart pointer.

In the scope ,How can I release the TheObj_Ptr 's memory?

like this;

{ // Open scope
TimeOfDayPtr tod = new TimeOfDayI; // Allocate instance
tod->second = 0; // Initialize
tod->minute = 0;
tod->hour = 0;
// Use instance...


//How can I release the TheObj_Ptr 's memory in this scope ??
tod=NULL;//Is OK?
}

Comments

  • marc
    marc Florida
    You don't need to release an object managed by smart pointers at all. When the smart pointer goes out of scope, the object it points to will automatically be deleted (provided that there is no other Ptr to the same object). So in your example the last line in the scope that sets tod to NULL is not necessary.
  • I know the smart pointer can release automatism,when he left the scope.
    Sometime ,I want to clean up the memeory manually, when the main program is not stop( the scope is global or the object that include this smart pointer not release).
    That is, I want to konw the method that manual releae smart pointer?
  • marc
    marc Florida
    You must set all smart pointers that point to an object to null explicitly. Then the object will be deleted.
  • OK.
    Thanks a lot.
  • I set OBj_Ptr=NULL,but how can proved effective?
    With the task explorer in win2003(or win2k),I feel the memory is leak!?
  • marc
    marc Florida
    Put a print statement in the destructor, or set a debugger breakpoint in the descructor. Then you know exactly when an object is deleted.
  • When the Obj_Ptr's _ref = 5 or 6 ,how can I release this memory once?
  • marc
    marc Florida
    Originally posted by level
    When the Obj_Ptr's _ref = 5 or 6 ,how can I release this memory once?

    You cannot. You have to set all smart pointers that still point to the object to null. That's how counted smart pointers work, i.e., the pointed-to object gets deleted when the reference count becomes zero. (This is nothing Ice specific, this is how all reference-counted smart pointers work.)
  • the example is very simple.

    .ice
    ....

    class Simple

    {

    string message;

    };



    class Simple2 extends Simple

    {

    Simple one;

    };


    ....
    First I create 3 Simple2,and their one is NULL,
    Second I create 3 Simple2, and their one is index =1,2,3;

    when the function end, Only 3 simple2 release .


    .......................



    class SimpleNow2 : virtual public ::Simple2
    {

    public:

    SimpleNow2()

    {};

    ~SimpleNow2()

    {

    if (one!=NULL) {
    one=NULL;
    }
    MessageBox(0,"","",0);
    };

    };






    void test()

    {

    std::vector<::Ice::ObjectPtr>mlist;



    for(long i=0;i<3;i++)

    {
    Simple2Ptr obj=new SimpleNow2;

    obj->one=NULL;

    mlist.push_back(obj);

    }



    for(long i=0;i<3;i++)

    {
    ::Ice::ObjectPtr obj=new Simple2;

    ::Ice::ObjectPtr plugin=mlist;

    Simple2Ptr obj2=Simple2Ptr::dynamicCast(plugin);
    Simple2Ptr obj3=Simple2Ptr::dynamicCast(obj);

    obj3->one=obj2;

    mlist.push_back(obj3);

    }




    for(long i=0;i<6;i++)


    {
    ::Ice::ObjectPtr obj=mlist;
    long ref=obj->__getRef();
    obj=NULL;

    }


    }
    .......................

    How can I do?
  • marc
    marc Florida
    This code has no effect:
    ::Ice::ObjectPtr obj=mlist[i];
    long ref=obj->__getRef();
    obj=NULL;
    
    With the code above you create a copy of a reference counted smart pointer, and then set this copy to null. You must set the original to null:
    ::Ice::ObjectPtr obj=mlist[i];
    long ref=obj->__getRef();
    mlist[i]=0;
    
    Or even simpler:
    mlist.clear();
    
  • Thanks.