Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #2996 > unrolled thread

Destructors can copying objects

Started byRuben Safir <ruben@mrbrklyn.com>
First post2011-04-02 12:58 +0000
Last post2011-04-05 20:37 +0000
Articles 5 on this page of 45 — 16 participants

Back to article view | Back to comp.lang.c++


Contents

  Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-02 12:58 +0000
    Re: Destructors can copying objects Volker Lukas <vlukas@gmx.de> - 2011-04-02 15:29 +0200
      Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-03 02:59 +0000
        Re: Destructors can copying objects "Balog Pal" <pasa@lib.hu> - 2011-04-03 11:32 +0200
          Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-03 12:29 +0100
            Re: Destructors can copying objects Bart van Ingen Schenau <bart@ingen.ddns.info> - 2011-04-04 13:43 +0200
              Re: Destructors can copying objects Michael Doubez <michael.doubez@free.fr> - 2011-04-05 01:26 -0700
            Re: Destructors can copying objects Joshua Maurice <joshuamaurice@gmail.com> - 2011-04-04 14:03 -0700
              Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:10 +0000
              Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:28 +0100
            Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:07 +0000
              Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:31 +0100
                Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:43 +0100
                Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 22:38 +0000
                  Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 23:42 +0100
                  Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 22:54 +0000
        Re: Destructors can copying objects James Kanze <james.kanze@gmail.com> - 2011-04-03 10:07 -0700
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:14 +0000
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:17 +0000
            Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-05 01:52 -0400
      Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-03 03:08 +0000
        Re: Destructors can copying objects Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-03 02:11 -0500
        Re: Destructors can copying objects Volker Lukas <vlukas@gmx.de> - 2011-04-03 12:02 +0200
        Re: Destructors can copying objects Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-03 12:14 +0200
          Re: Destructors can copying objects Roberto Waltman <usenet@rwaltman.com> - 2011-04-03 18:16 -0400
            Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-15 23:06 +0000
              Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-15 19:19 -0400
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-15 23:03 +0000
            Re: Destructors can copying objects "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-16 01:47 +0200
              Re: Destructors can copying objects Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-16 13:00 +0200
                Re: Destructors can copying objects "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-16 15:57 +0200
    Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-02 09:51 -0400
    Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-04 12:56 +0000
      Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:05 +0000
        Re: Destructors can copying objects Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-04 21:56 -0500
        Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-05 11:36 +0000
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 15:40 +0000
            Re: Destructors can copying objects Sana <sanatakos@gmail.com> - 2011-04-05 10:11 -0700
            Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 20:28 +0000
            Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-06 11:12 +0000
        Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 13:02 +0000
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 15:51 +0000
            Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 17:47 +0000
              Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 20:25 +0000
                Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 20:37 +0000

Page 3 of 3 — ← Prev page 1 2 [3]


#3152

Fromdrew@furrfu.invalid (Drew Lawson)
Date2011-04-05 13:02 +0000
Message-ID<inf3t5$21uh$2@raid.furrfu.com>
In reply to#3107
In article <indbq3$r87$2@reader1.panix.com>
	Ruben Safir <ruben@mrbrklyn.com> writes:

>The purpose of the question is to understand what is happening, not to necessarily
>fix the program.  This is not a homework assignment with a deadline.  I just want to
>have a better understanding of why the destructor caused a double delete.

Others will cover (or have covered) the language intricacies.
The practical apect of that question is roughtly:

Your class Foo has a member
  Bar* ptr;
The destructor does
  delete ptr;

If you let the compiler generate the copy constructor and do this:
  Foo a; // assuming ptr points to valid allocated memory
  Foo b;
  b = a;
The default copy constructor will copy a.ptr to b.ptr, because a
pointer is just a bunch of bytes.

When a.~Foo() runs, a.ptr is deleted.
When b.~Foo() runs, b.ptr, which is the same as a.ptr and was already
deleted, gets deleted.

So, if the primitive types in the object have more meaning than
just "a bunch of bytes," you probably need to write the copy
constructor (and operator=), or prevent copying.

-- 
Drew Lawson            |  Savage bed foot-warmer
                       |    of purest feline ancestry
                       |  Look out little furry folk
                       |    it's the all-night working cat

[toc] | [prev] | [next] | [standalone]


#3167

FromRuben Safir <ruben@mrbrklyn.com>
Date2011-04-05 15:51 +0000
Message-ID<infdqj$hvf$2@reader1.panix.com>
In reply to#3152
On Tue, 05 Apr 2011 13:02:29 +0000, Drew Lawson wrote:


> When a.~Foo() runs, a.ptr is deleted. When b.~Foo() runs, b.ptr, which
> is the same as a.ptr and was already deleted, gets deleted.

Right.  I tried to design the destructor to not to do that, but I 
obviously failed, but you explained this well.  I tried to test if the 
pointer is pointing to something valid before deleting it, but it fails.  
It seems that deleting the pointer to an object doesn't actually set it 
to null, nor do I have an expression that I know of which can test if it 
points to valid data.  I know, that is a fundamental pointer C thing that 
I should be aware of.

#include<iostream>
#include<string>

class obj{
   public:
      obj():a(10), b("Hello World") {}
      int a;
      std::string b;
};

class myobj{

public:
myobj(){
   b = myfunct();
}

~myobj();
obj * b;

obj * myfunct(){
	obj* a = new obj;
	return a;
}
};
myobj::~myobj(){
	std::cerr << "Weeeee" <<std::endl; 
	delete b;
}

int main(int argv, char ** argc){

    obj * out;
    obj * out2;
    obj * out3 = new obj;

    myobj * parent = new myobj;
    out = parent->myfunct();
    out2 = parent->b;

   std::cout << "Object Addreses: " << out << " " << out2 << " " << out3 << std::endl;
   std::cout << "out " << out->a << " " << out->b << std::endl;
   std::cout << "out2 " << out2->a << " " << out2->b << std::endl;
   std::cout << "out3 " << out3->a << " " << out3->b << std::endl;
   delete parent; //troube
   std::cout <<"Object Addreses: " <<  out << " " << out2 <<" " << out3  << std::endl;
   delete out; //serious trouble
   std::cout <<"Object Addreses: " <<  out << " " << out2 << " " << out3 << std::endl;

   delete out3; //serious trouble
   std::cout <<"Object Addreses: " <<  out << " " << out2 << " " << out3 << std::endl;

  return 0;
}

[toc] | [prev] | [next] | [standalone]


#3176

Fromdrew@furrfu.invalid (Drew Lawson)
Date2011-04-05 17:47 +0000
Message-ID<infkik$28sd$1@raid.furrfu.com>
In reply to#3167
In article <infdqj$hvf$2@reader1.panix.com>
	Ruben Safir <ruben@mrbrklyn.com> writes:
>On Tue, 05 Apr 2011 13:02:29 +0000, Drew Lawson wrote:
>
>
>> When a.~Foo() runs, a.ptr is deleted. When b.~Foo() runs, b.ptr, which
>> is the same as a.ptr and was already deleted, gets deleted.
>
>Right.  I tried to design the destructor to not to do that, but I 
>obviously failed, but you explained this well.  I tried to test if the 
>pointer is pointing to something valid before deleting it, but it fails.  
>It seems that deleting the pointer to an object doesn't actually set it 
>to null,

It wouldn't matter if it did, in this case.
a.ptr and b.ptr are two different pointers.  If
  delete a.ptr;
cleared the pointer, it would have no way of knowing about b.ptr.

>nor do I have an expression that I know of which can test if it 
>points to valid data.  I know, that is a fundamental pointer C thing that 
>I should be aware of.

It is a personal annoyance when trying to track down pointer errors.
It would be convenient for debug code to have something to test to
say "this can be dereferenced," but even that isn't available in a
portable way.

And in this case, the pointer value *is* valid.  It just happens
to be something that the allocator thinks is unused.


-- 
Drew Lawson            | "Look!  A big distracting thing!"
                       |     -- Crow T. Robot.
                       |

[toc] | [prev] | [next] | [standalone]


#3185

FromRuben Safir <ruben@mrbrklyn.com>
Date2011-04-05 20:25 +0000
Message-ID<inftr3$hvf$3@reader1.panix.com>
In reply to#3176
On Tue, 05 Apr 2011 17:47:00 +0000, Drew Lawson wrote:

> It wouldn't matter if it did, in this case. a.ptr and b.ptr are two
> different pointers.  If
>   delete a.ptr;
> cleared the pointer, it would have no way of knowing about b.ptr.

when you do a delete, it doesn't seem to delete the pointer, but the memory
at the address that the pointer is storing.  I could be wrong, but that
is the basis of the double delete.
...

I see.  Your right.  is b.ptr is wiped to null, a.ptr is still pointing to 
a dellocated part of the heap.  But if the pointers are passed by reference,
a.ptr and b.ptr are actually the same pointer inside a single object.

[toc] | [prev] | [next] | [standalone]


#3187

Fromdrew@furrfu.invalid (Drew Lawson)
Date2011-04-05 20:37 +0000
Message-ID<infuie$2cmp$1@raid.furrfu.com>
In reply to#3185
In article <inftr3$hvf$3@reader1.panix.com>
	Ruben Safir <ruben@mrbrklyn.com> writes:
>On Tue, 05 Apr 2011 17:47:00 +0000, Drew Lawson wrote:
>
>> It wouldn't matter if it did, in this case. a.ptr and b.ptr are two
>> different pointers.  If
>>   delete a.ptr;
>> cleared the pointer, it would have no way of knowing about b.ptr.
>
>when you do a delete, it doesn't seem to delete the pointer, but the memory
>at the address that the pointer is storing.  I could be wrong, but that
>is the basis of the double delete.
>...
>
>I see.  Your right.  is b.ptr is wiped to null, a.ptr is still pointing to 
>a dellocated part of the heap.  But if the pointers are passed by reference,
>a.ptr and b.ptr are actually the same pointer inside a single object.

I may have accidentally confused you.  (My phrasings aren't always
as clear as I believe they are.)

You are correct that delete frees the memory and does not clear the
pointer handed to it.

I was pointing out that in the case of a copied pointer, it wouldn't
help even if delete *did* clear the pointer because it couldn't
clear the other pointer in the other object.


-- 
Drew Lawson            | "Look!  A big distracting thing!"
                       |     -- Crow T. Robot.
                       |

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c++


csiph-web