Solved

deleting object instance

Posted on 2001-08-04
23
272 Views
Last Modified: 2010-04-02
hi,

I have the sort of code snippet below and am curious something about it.
As obviously seen, i am creating a new window object instance and immediately
deleting it, but it's still possible to call the member functions of the object.


void CMenusDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
     CDialog w;
     delete w;
     w.Create( .. );
     w.FlashWindow( TRUE );
        ...

        ...
}


what the heck is this?. i think that instance creation in the local scope
makes the object permanent although the delete operator used. is it safe to use like
this way? i'm confused a little, what do you comment on it?


Thanks in advance.
Osman Mavideniz
0
Comment
Question by:omavideniz
  • 10
  • 7
  • 2
  • +3
23 Comments
 
LVL 3

Expert Comment

by:jtm111
ID: 6352426
Stroustrup notes that in the C++ standard, access to an object after it has been deleted is undefined.

It doesn't surprise me that you could run instructions after deleting an object. But it is absolutely bad practice. Your code is not portable because you don't know how C++ compilers will handle your code.

Never use an object after you delete it.
0
 

Author Comment

by:omavideniz
ID: 6352435
hi jtm111,

my problem is not about using an object after deleting it, i don't want to make use of it. but the concept of local instance creation and destruction, that if you create a dynamic object instead;

CDialog w = new CDialog();
delete w;    

method calls now crash, because of the obvious destruction of the object but why not the same thing applies the previous one...
0
 
LVL 3

Expert Comment

by:jtm111
ID: 6352462
Oh I see... You aren't creating the object with new. I was focusing on the delete line.

In the C++ standard, delete only has an effect for a pointer returned by new or to 0 which has no effect (Stroustrup, p. 128).

I would expect a compiler warning or error if you try to delete anything other than a pointer or 0. Didn't you get one?

If not, I suspect it simply ignored the delete operator, in which case your program would run.

0
 
LVL 22

Expert Comment

by:nietod
ID: 6352504
>> I would expect a compiler warning or
>> error if you try to delete anything
>> other than a pointer or 0.
So would I!!

That code absolutely should not compile.  
0
 

Author Comment

by:omavideniz
ID: 6352863
that code definitely compiles within VC++ 6.0 and no errors or warnings raised.
0
 

Author Comment

by:omavideniz
ID: 6352865
that code definitely compiles within VC++ 6.0 and no errors or warnings raised.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6352904
The code definitely does not compile in vc 6.0.  I tried it.

Are you sure you are compiling this code?  perhaps the file you have is not included in the project.  Place a blantent error in the code (although you really have) and see what happens, like

  CDialog w;
  delete w;
  this line surely is a compiler error;
0
 
LVL 32

Expert Comment

by:jhance
ID: 6353012
>>this line surely is a compiler error;

You would think so but it's not flagged as an error in VC++ 6.0!!  Try it....

0
 

Author Comment

by:omavideniz
ID: 6353028
as jhance stated its neither an error nor warning in VC++ 6.0.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6353038
Huh?  I did try it.  I got two errors.  c2059 and c2238

This makes no sense?
0
 

Author Comment

by:omavideniz
ID: 6353049
actually, i'm curious about how vc++ handles to prevent the delete to destroy the local object. is it using some smart technique to determine the local objects and prevent their destruction, or the c++ language concept provides this mechanism implicitly to prohibit the local object destruction.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:nietod
ID: 6353054
Are you sure you guys are compiling this.  It is clearly a syntax error.  It is one that VC reports for all cases I can construct (any non-pointer) as well as this specific case

the only think I can think if is that an implicit conversion to some pointer type is being called on the object.  But the object doesn't define any pointer conversions that I am aware of, and even if it does, no conversion should be invoked by the compiler in this case.  So that hardly seeems like that is what is happening, but it the only think I can think of.  (And it doesn't work for me!)
0
 
LVL 32

Expert Comment

by:jhance
ID: 6353061
>>This makes no sense?

Agreed!  I'm 100% positive that I'm compiling it.  And I get no errors or warnings at all on it.  I even bumped the C++ warning level up to 4 and nothing.

Could this be a VC++ version issue?  Like some rev levels have a problem with this and others don't?  I think I have VC++ SP3 installed but I'm not sure about it..  It might be a later one.
0
 

Author Comment

by:omavideniz
ID: 6353068
the explanation of the errors you reported are below and makes no sense of our subject. may be some of our compiler settings causes the difference, i'm not sure why we are encountering different compiler behaviours.

----------
Compiler Error C2059
syntax error : 'token'

The token caused a syntax error. Problems of this type can sometimes be attributed to a syntactical or clerical error. For example:

void main (             // No closing parenthesis.
{

}

----------
Compiler Error C2238
unexpected token(s) preceding 'token'

An incorrect token, or tokens, was found before the specified token.

This error can be caused by an invalid name in a bit-field declaration.

The following is an example of this error:

struct bits
{
   int field1 : 16;
   int 9      : 16;  // error, 9 is not a valid name
};


0
 
LVL 22

Expert Comment

by:nietod
ID: 6353070
>> actually, i'm curious about how vc++
>> handles to prevent the delete to destroy
>> the local object. is it
>> using some smart technique to determine
>> the local objects and prevent their
>> destruction,
Nope.  delete on an non-pointer type is a syntax error.  it does not matter where it comes from (local or heap).   Delete on a pointer is never a syntax error.  Now if the pointer was not allocated with new, then the delete operation is a semantic error, but not a syntax error and the compiler will not complain about it, but the program behavior is undefined--and rarely good!

So for example

int *iptr = new int;

delete *iptr; // syntax error.  does not compile.  this
         // int is from the heap, but you cannot delete
         // an int, only a pointer.  


int i - 5;
delete &i; // This compiles.  But the behavior
    // is undefined.  Probably causes a crash.

>> Could this be a VC++ version issue?
Its possible, but seems unlikely.  Though nothing else seems more likely.

Can you guts try this other other data types?  What does

int i;
delete i;

do?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6353073
>> explanation of the errors you reported
>> are below and makes no sense of our subject
yes they do.  The first is the error I would expect.  The 2nd is a sideffect of it trying to continue after the first error.  
0
 

Author Comment

by:omavideniz
ID: 6353131
i tried it for int and get the error,

Compiler Error C2541
delete : cannot delete objects that are not pointers

may be cause of a vc++ issue it doesn't expose the same error for user-defined objects.

0
 
LVL 22

Expert Comment

by:nietod
ID: 6353313
Strange that I don't get C2541 when I do get an error for the class then.   I need to do a bit of research here...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6353320
Now this must be a version issue.  When I do
 int i;
 delete i;

I get the same erros as when I try to delete the object, that is, I get C2059 and C2238 in both cases.

I can't do the next test though.

Can you step into the delete of this object in the debugger.  Look to see if a conversion operator for the CDialog class is being called.  I am 99.9% sure that is not allowed, but it mgiht be a mistake in the VC compiler that then allows this code to compile.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6355252
CDialog (from CWnd) defines operator HWND, which is a void* (I think - sometimes, can be a struct if STRICT is defined?, in which case it won't compile?), hence this compiles, as an example, this also compiles:

class X
{
public:
    operator void*()
    {
        return 0;
    }
};

int main()
{
    X theX;
    delete theX;
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 6355311
So it is a conversion?   I didn't think that was allowed.  I have to look into that I'm not sure what the rules are for that.   Like if a class has two pointer conversions, which one would delete use, or would then be ambiguous.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 6355412
Okay for delete you are allowed one conversion to a pointer in a  class.  The standard says

*********************************

1 The  delete-expression  operator  destroys  a  most   derived   object
  (_intro.object_) or array created by a new-expression.
          delete-expression:
                  ::opt delete cast-expression
                  ::opt delete [ ] cast-expression
  The  first alternative is for non-array objects, and the second is for
  arrays.  The operand shall have a pointer type, or a class type having
  a  single  conversion  function  (_class.conv.fct_) to a pointer type.

***********************************8
so you are allowed to call delete on an object if its class defines a single conversion function to a pointer.  CDialog provides a conversion to an HWND.  If STRICT is not defined this is a pointer, and so the code will compiler.

But how what happens in this case?  Well delete does not delete the CDialog you specify.  (which woudl be a bad thing.)  The CDialog conversion function returns a window handle.   If the dialog is closed this handle is NULL, so the delete is performed on a NULL pointer, which does nothing, and thuse the code is fine.  In this case the CDialog is not deleted and nothing else is deleted.  No harm is done.  But if the dialog is open, the the covnersion returns a non-NULL pointer.  Delete will execute on this pointer.  But this is not a pointer to an object allocated fromt he C++ heap and thus this will cause unpredictable behavior.   This is bad.
0
 

Expert Comment

by:amp072397
ID: 6783787
omavideniz:

You have many open questions:

http://www.experts-exchange.com/jsp/qShow.jsp?qid=20222327
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20261522
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20255833
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20248161
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20247596
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20247451
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20239442
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20235419
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20234355
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20230738
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20227630
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20210091
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20163508
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20163400
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20156064
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20151257
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20149551
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20107807
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20107283
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20096836
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20095000
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20094242
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20081432
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20068029
http://www.experts-exchange.com/jsp/qShow.jsp?qid=12040399
http://www.experts-exchange.com/jsp/qShow.jsp?qid=12040320

To assist you in your cleanup, I'm providing the following guidelines:

1.  Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.

2.  Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.

3.  When grading, be sure to read:
http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.

4.  Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS:  http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.

5.  If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.

Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!


thanks!
amp
community support moderator

2/6
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now