473,748 Members | 4,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1997

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**********@a tlantis.news.tp i.pl>, R.Z.
<re************ ******@gazeta.p l> 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**********@n emesis.news.tpi .pl>, R.Z.
<re************ ******@gazeta.p l> 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
1728
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 : throw 5;"). but "delete" neither does exit the scope nor has a return value. any idea? thanks,
13
3111
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" automatically after programs do "delete p"?
8
25073
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 different table. While I am using the tools in Access for query setup, its easier to show it on here using the SQL for the query, which is as follows( the table is ): DELETE .date, .,
1
1380
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. Here is the code I'm using Dim imgShow As New ImageButton imgShow.CommandName = "Delete" imgShow.CommandArgument = arrayOfFiles(i) AddHandler imgShow.Command, AddressOf Image_Command
2
2062
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 example, the delete operation has no effect: var z = <abc><foo a='1'>1</foo><foo a='2'>2</foo></abc>; alert(z.foo.(@a == '2')); delete z.foo.(@a == '2');
5
8091
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 assigned to <pointerby "new". Does "new" create a list of pointer names and the size of the memory array they point to? I also read that some compilers may store the number of consec mem locations a pointer points to, just before the first data...
1
1682
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
3838
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 the end of the app for example: class test { public: int x;
3
35945
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?. Main.cpp:377: undefined reference to `operator delete(void*)' tinyXML/include/tinystr.h:259: undefined reference to `operator delete(void*)' ../common/tinyXML/include/tinyxml.h:1401: undefined reference to `operator delete(void*)'...
0
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9243
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.