CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2013
    Posts
    12

    [RESOLVED] Regarding new operator.

    Hi,

    Please go through the below code.

    Code:
    class PlotDraw
    {
    
    public : 
        Graphics::TBitmap *DrawnBitmap;
        
        PlotDraw(TComponent *Owner);
       ~PlotDraw();
        Initialise();
        Drawing(TObject *Sender);
    
    
    };
    
    PlotDraw::PlotDraw(TComponent *Owner)
    {
        
    
            Initialise(); 
    
    
    }
    
    
    PlotDraw::~PlotDraw()
    {
             delete DrawnBitmap;
    }
    
    PlotDraw::Initialise()
    {
         DrawnBitmap = new Graphics::TBitmap;
        // Other initialisation code
         
    }
    
    // method calls with the various events
    PlotDraw::Drawing(TObject *Sender)
    {
        delete DrawnBitmap;  
        Initialse();
    }

    In the above, method Drawing() calls on when an event trigger.
    Every time it deletes the memory (data) and allocate "new" memory in Initialisation() method.

    What will happen, If I don't delete the allocated memory before calling Initialise() in Drawing() method ?
    Is it a good idea to delete the memory and re allocate with a new operator?

    Thanks in advance.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Regarding new operator.

    Quote Originally Posted by srvolatile View Post
    What will happen, If I don't delete the allocated memory before calling Initialise() in Drawing() method ?
    You will have a memory leak.
    Quote Originally Posted by srvolatile View Post
    Is it a good idea to delete the memory and re allocate with a new operator?
    In that order, no, because it is not exception safe. If the allocation or object creation fails, you cannot restore the object that you just deleted.
    Whether the approach of creating a new object and deleting the old one is better than reusing the same object depends on the use case. You didn't provide enough details to say anything specific.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Regarding new operator.

    Quote Originally Posted by srvolatile View Post
    Hi,
    Please go through the below code.
    What if Initialize() fails for some reason, and then your object is destroyed? You now have a double "delete" error, since you called delete before the Initialize() call, and you're calling delete a second time in the destructor.

    In this day and age of C++, there is no such need for the back-and-forth, spaghetti-like coding of operator new in one function, and delete in another function. What you need is a reference-counted smart pointer, possibly shared_ptr(), so that the real deletion only occurs when it needs to.
    Code:
    #include <memory>
    
    class PlotDraw
    {
        typedef std::shared_ptr<Graphics::TBitmap> BitmapPtr;
        public : 
            BitmapPtr DrawnBitmap;
            PlotDraw(TComponent *Owner);
           ~PlotDraw();
            Initialise();
            Drawing(TObject *Sender);
    };
    
    PlotDraw::PlotDraw(TComponent *Owner) : DrawnBitmap(new Graphics::TBitmap)  // initial allocation
    {
        // To get the TBitmap pointer
        // Graphics::TBitmap* pBitmap = DrawnBitmap.get();
        Initialise(); 
    }
    
    PlotDraw::Initialise()
    { 
       // no need to allocate
    }
    
    PlotDraw::~PlotDraw
    {
      // no need to call delete explicitly.  If the smart pointer reference count is not 0, 
      // it will call delete for you, otherwise, it won't call delete
    }
    
    // method calls with the various events
    PlotDraw::Drawing(TObject *Sender)
    {
        DrawnBitmap.reset(new Graphics::TBitmap);  // reallocation takes place here
        Initialse();
    }
    The question is still whether you need to reallocate each time, but the code will not have the double deletion error you would have gotten. To get the internal pointer, use the .get() method on DrawnBitmap. Note that there are no calls whatsoever to delete. The shared_ptr takes care of all of these details.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Feb 2013
    Posts
    12

    Re: Regarding new operator.

    Hi D Drmmr,

    Thanks for your reply and got my answer.

  5. #5
    Join Date
    Feb 2013
    Posts
    12

    Re: Regarding new operator.

    Hi Paul McKenzie,

    your code is alot better than mine.
    Thanks for your suggestion.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured