473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

is "delete" needed?

i'm using a class from some api that is said to automatically call its
destructor when its out of scope and deallocate memory. i create instances
of this class using "new" operator. do i have to explicitly call delete on
these instances when i no longer need them?
Aug 9 '05 #1
6 1978

R.Z. wrote:

[]
i create instances
of this class using "new" operator. do i have to explicitly call delete on
these instances when i no longer need them?


Yes, you do. Or better, use a smart pointer such as std::auto_ptr that
calls delete for you.

Aug 9 '05 #2
In message <dd**********@atlantis.news.tpi.pl>, R.Z.
<re******************@gazeta.pl> writes
i'm using a class from some api that is said to automatically call its
destructor when its out of scope and deallocate memory.
Is that really what the documentation says? When _any_ automatic object
goes out of scope its destructor is automatically called. OTOH objects
created with "new" are not automatic, and don't "go out of scope".
i create instances
of this class using "new" operator. do i have to explicitly call delete on
these instances when i no longer need them?

Does the API provide some other means (e.g. a "release" function) for
telling the instances they are no longer needed? APIs for
reference-counted objects commonly provide functions called something
like AddRef, which increases the reference count, and Release, which
decrements it and executes "delete this" if it becomes zero. If that's
the case, you must not use delete, as Release() does it for you. But
such objects are necessarily created on the heap, so the concept of
"going out of scope" doesn't really apply.

Or are they talking about some kind of smart pointer, such that the
pointer's destructor deletes the object when the pointer goes out of
scope?

--
Richard Herring
Aug 9 '05 #3
this class is a template intelligent array class. here's what the docs say:
Tabs may be used on the stack, i.e. they may be declared as a local variable
of a function or method. You can set the number of elements in the table,
work with them, and then when the function returns, the destructor of the
Tab is called, and the memory will be deallocated.

Tabs are only appropriate for use with classes that don't allocate memory.
For example, Tab<float> is fine while Tab<TSTR> is problematic (TSTR is the
class used for strings in 3ds max). In this case, the TSTR class itself
allocates memory for the string. It relies on its constructor or destructor
to allocate and free the memory. The problem is the Tab class will not call
the constructors and destructors for all the items in the table, nor will it
call the copy operator. As an example of this, when you assign a string to
another string, the TSTR class does not just copy the pointer to the string
buffer (which would result in two items pointing to the same block of
memory). Rather it will allocate new memory and copy the contents of the
source buffer. In this way you have two individual pointers pointing at two
individual buffers. When each of the TSTR destructors is called it will free
each piece of memory. So, the problem with using a Tab<TSTR> is that when
you assign a Tab to another Tab, the Tab copy constructor will copy all the
elements in the table, but it will not call the copy operator on the
individual elements. Thus, if you had a Tab<TSTR> and you assigned it to
another Tab<TSTR>, you'd have two TSTRs pointing to the same memory. Then
when the second one gets deleted it will be trying to double free that
memory.

So again, you should only put things in a Tab that don't allocate and
deallocate memory in their destructors. Thus, this class should not be used
with classes that implement an assignment operator and or destructor because
neither are guaranteed to be called. The way around this is to use a table
of pointers to the items.
Aug 9 '05 #4
In message <dd**********@nemesis.news.tpi.pl>, R.Z.
<re******************@gazeta.pl> writes
this class is a template intelligent array class. here's what the docs say:
Tabs may be used on the stack, i.e. they may be declared as a local variable
of a function or method. You can set the number of elements in the table,
work with them, and then when the function returns, the destructor of the
Tab is called, and the memory will be deallocated.
So far, so good. Normal behaviour for a container (it sounds a bit like
std::vector.) If you allocate a Tab with "new" (though I can't see why
you'd want to) then it's your responsibility to delete it when you've
finished with it.
Tabs are only appropriate for use with classes that don't allocate memory.
For example, Tab<float> is fine while Tab<TSTR> is problematic (TSTR is the
class used for strings in 3ds max). In this case, the TSTR class itself
allocates memory for the string. It relies on its constructor or destructor
to allocate and free the memory. The problem is the Tab class will not call
the constructors and destructors for all the items in the table, nor will it
call the copy operator.
Not so good. It's like std::vector, but *worse*, because you can only
use it on PODs. Internally it's doing something like memcpy instead of
calling the contained class's copy constructor or assignment operator.
As an example of this, when you assign a string to
another string, the TSTR class does not just copy the pointer to the string
buffer (which would result in two items pointing to the same block of
memory). Rather it will allocate new memory and copy the contents of the
source buffer. In this way you have two individual pointers pointing at two
individual buffers. When each of the TSTR destructors is called it will free
each piece of memory.
So the TSTR is a string-like class, with normal deep-copy semantics. But
because of the deep copy you can't store it in a Tab.
So, the problem with using a Tab<TSTR> is that when
you assign a Tab to another Tab, the Tab copy constructor will copy all the
elements in the table, but it will not call the copy operator on the
individual elements. Thus, if you had a Tab<TSTR> and you assigned it to
another Tab<TSTR>, you'd have two TSTRs pointing to the same memory. Then
when the second one gets deleted it will be trying to double free that
memory.

So again, you should only put things in a Tab that don't allocate and
deallocate memory in their destructors. Thus, this class should not be used
with classes that implement an assignment operator and or destructor because
neither are guaranteed to be called. The way around this is to use a table
of pointers to the items.

Presumably Tab provides something you haven't described, that makes it
more than just being a container, or you'd be better off using
std::vector. Unlike Tab, it provides an intelligent array which clears
up after itself *and* can be used on any class with normal copy
semantics, deep or shallow.

--
Richard Herring
Aug 9 '05 #5
hi r.z,

as clearly mentioned in the above documentation, use this Tab class
only for the object data types and not for pointer datatypes.

A Tab class object releases the resources allocated with the Tab object
itself and DOES NOT releases the resources pointed by the pointers
stored in it.

If you still want to store the pointers to the objects you have to
manually call destructor on the pointers stored in the Tab class by
iterating over the Tab class object. But even then u have to take care
when u are assigning one tab object to another one.
thanks,
rt

Aug 9 '05 #6
ra************@gmail.com yazdi:
hi r.z,

as clearly mentioned in the above documentation, use this Tab class
only for the object data types and not for pointer datatypes.

A Tab class object releases the resources allocated with the Tab object
itself and DOES NOT releases the resources pointed by the pointers
stored in it.

If you still want to store the pointers to the objects you have to
manually call destructor on the pointers stored in the Tab class by
iterating over the Tab class object. But even then u have to take care
when u are assigning one tab object to another one.


I had to do something like this. Here is a trick:

Wrap the pointer with a non-destructive class
template <class Ptr>
class NDPtr
{
Ptr* p;
public:
template <class Ptr> NDPtr(const Ptr* p_=0) {p=p_;}
template <class Ptr> NDPtr(const Ptr& r) {p=r.p;}
template <class Ptr> void operator=(const Ptr& r) {p=r.p;}
template <class Ptr> ~NDPtr() {/*NOTHING, See Below*/}
....
};

Then make a destructive class which has nothing but a destructor that
deletes the wrapped pointer.
template <class Ptr>
class DPtr : public NDPtr<Ptr>
{
public:
template <class Ptr> ~DPtr() { if(p) delete p;}
}

NDPtr<Type> can be used with Tab class or with any container class.
For example
set<NDPtr<char> > objCharPtrSet;

// use it as long as you want

And when you want to delete all the pointers do this:

// we should treat NDPtr as DPtr
// to delete the wrapped pointer.
set<DPtr<char> >& r = (set<DPtr<char> >&) objCharPtrSet;

// call clear to delete all the pointers
r.clear();

thanks,
rt


Aug 11 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stefan Strasser | last post by:
why is delete an expression and not a statement? (in my draft copy of the standard). I was about to ask the same question about "throw" but found an expression case of throw("return boolvalue ? 5...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
8
by: John Baker | last post by:
Hi: Access 2000 W98! I have a table with numerous records in it, and am attempting to delete certain records that have been selected from it. These are selected based on the ID number in a...
1
by: Michael Ramey | last post by:
Hi, I'm dynamically creating a table of "delete" imagebuttons, that correspond to files on the webserver. I want to respond to clicks of these buttons, so I know to know what file to delete. ...
2
by: Bob Tinsman | last post by:
This problem shows up in Firefox 1.5.0.1 and Rhino 1.6R2. I've found that if I have an XML node expression that ends in a filter, I can't use it with the delete operator. In the following...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
1
by: MorgyTheMole | last post by:
I have a base class: class A { protected: static void* operator new (size_t size); void operator delete (void *p); public: void Delete(); }
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
3
by: tvnaidu | last post by:
I compiled tinyxml files (.cpp files) and when I am linking to create final exe in Linux, I am getting lot of errors saying "undefiend reference" to "operator new" and "delete", any idea?. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.