[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.
Re: Regarding new operator.
Quote:
Originally Posted by
srvolatile
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
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.
Re: Regarding new operator.
Quote:
Originally Posted by
srvolatile
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
Re: Regarding new operator.
Hi D Drmmr,
Thanks for your reply and got my answer.
Re: Regarding new operator.
Hi Paul McKenzie,
your code is alot better than mine.
Thanks for your suggestion.