Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > destruction of already destructed pointer variable when copying anobject - abort error

Reply
Thread Tools

destruction of already destructed pointer variable when copying anobject - abort error

 
 
suresh
Guest
Posts: n/a
 
      09-11-2010
Hi,
Kindly consider the code segment below: I have a function object
containing a pointer variable which is passed to min_element
algorithm. My problem is the pointer variable is deleted twice and I
do not know how to fix this issue.

class FO{
public:
set<int>::size_type size(){return s->size()}
bool operator()(int a, int b);
~FO();

private:
set<int> * s;
};

FO::FO(){
s = new set<int>;
}

FO::~FO(){
delete s;
}

bool FO:perator()(int a,int b){
s->insert(a);
return (a<b);
}

int main(){
vector<int> v;
//vector populated
FO fo;
min_element(v.begin(),v.end(),fo);
}

The variable 's' is defined as a pointer bcz min_element algorithm
takes a copy of its function object argument. Now inside the
min_element algorithm, the copy of 'fo' is deleted which results in
freeing of the memory associated with 's'. But in the main, the
original object fo is destructed and then also the same memory is
freed and this gives a abort error.

How to solve this kind of a problem?

thanks
suresh
 
Reply With Quote
 
 
 
 
Garrett Hartshaw
Guest
Posts: n/a
 
      09-11-2010
> Hi,
> Kindly consider the code segment below: I have a function object
> containing a pointer variable which is passed to min_element
> algorithm. My problem is the pointer variable is deleted twice and I
> do not know how to fix this issue.
>
> class FO{
> public:
> set<int>::size_type size(){return s->size()}
> bool operator()(int a, int b);
> ~FO();
>
> private:
> set<int> * s;
> };
>
> FO::FO(){
> s = new set<int>;
> }
>
> FO::~FO(){
> delete s;
> }
>
> bool FO:perator()(int a,int b){
> s->insert(a);
> return (a<b);
> }
>
> int main(){
> vector<int> v;
> //vector populated
> FO fo;
> min_element(v.begin(),v.end(),fo);
> }
>
> The variable 's' is defined as a pointer bcz min_element algorithm
> takes a copy of its function object argument. Now inside the
> min_element algorithm, the copy of 'fo' is deleted which results in
> freeing of the memory associated with 's'. But in the main, the
> original object fo is destructed and then also the same memory is
> freed and this gives a abort error.
>
> How to solve this kind of a problem?


I'm no expert, but I would say that a smart pointer (e.g.
std::tr1::shared_pointer) would probably be your best choice.

-Garrett
 
Reply With Quote
 
 
 
 
Gil
Guest
Posts: n/a
 
      09-11-2010
On Sep 10, 11:34*pm, suresh <suresh.amritap...@gmail.com> wrote:
> Hi,
> Kindly consider the code segment below: I have a function object
> containing a pointer variable which is passed to min_element
> algorithm. My problem is the pointer variable is deleted twice and I
> do not know how to fix this issue.
>
> class FO{
> public:
> set<int>::size_type size(){return s->size()}
> bool operator()(int a, int b);
> ~FO();
>
> private:
> set<int> * s;
>
> };
>
> FO::FO(){
> s = new set<int>;
>
> }
>
> FO::~FO(){
> delete s;
>
> }
>
> bool FO:perator()(int a,int b){
> s->insert(a);
> return (a<b);
>
> }
>
> int main(){
> vector<int> v;
> //vector populated
> FO fo;
> min_element(v.begin(),v.end(),fo);
>
> }
>
> The variable 's' is defined as a pointer bcz min_element algorithm
> takes a copy of its function object argument. Now inside the
> min_element algorithm, the copy of 'fo' is deleted which results in
> freeing of the memory associated with 's'. But in the main, the
> original object fo is destructed and then also the same memory is
> freed and this gives a abort error.
>
> How to solve this kind of a problem?
>
> thanks
> suresh


solution no. 89 to your question:

/*
* func_obj.cpp
*
* Created on: Sep 11, 2010
* Author: Gill
*/

#include <iostream>
#include <iterator>
#include <set>
#include <vector>
#include <algorithm>
#include <tr1/functional>
#include <cstdlib>

struct func_obj : public std::binary_function< int, int, bool > {

typedef std::set< int >::const_iterator set_citerator_t;

public:
bool operator( )( int a, int b ) {
s_.insert( a );
return ( a < b );
}

std::set< int >::size_type size( ) {
return s_.size( );
}

set_citerator_t begin( ) const {
return s_.begin( );
}

set_citerator_t end( ) const {
return s_.end( );
}

private:
std::set< int > s_;

};

int main( ) {
std::vector< int > v;
//vector populated
std::generate_n( back_inserter( v ), 10, std::rand );

func_obj fo;

std::min_element( v.begin( ), v.end( ), std::tr1::ref( fo ) );
std::copy( fo.begin( ),
fo.end( ),
std:stream_iterator< int >( std::cout, " " ) );

std::cout << std::endl;

return 0;
}

 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      09-11-2010
On Sep 11, 5:34*am, suresh <suresh.amritap...@gmail.com> wrote:
> Hi,
> Kindly consider the code segment below: I have a function object
> containing a pointer variable which is passed to min_element
> algorithm. My problem is the pointer variable is deleted twice and I
> do not know how to fix this issue.
>
> class FO{
> public:
> set<int>::size_type size(){return s->size()}
> bool operator()(int a, int b);
> ~FO();
>
> private:
> set<int> * s;
>
> };
>
> FO::FO(){
> s = new set<int>;
>
> }
>
> FO::~FO(){
> delete s;
>
> }
>
> bool FO:perator()(int a,int b){
> s->insert(a);
> return (a<b);
>
> }
>
> int main(){
> vector<int> v;
> //vector populated
> FO fo;
> min_element(v.begin(),v.end(),fo);
>
> }
>
> The variable 's' is defined as a pointer bcz min_element algorithm
> takes a copy of its function object argument. Now inside the
> min_element algorithm, the copy of 'fo' is deleted which results in
> freeing of the memory associated with 's'. But in the main, the
> original object fo is destructed and then also the same memory is
> freed and this gives a abort error.
>
> How to solve this kind of a problem?



1. understand "the rule of three" (http://en.wikipedia.org/wiki/
Rule_of_three_%28C%2B%2B_programming%29)
2. class FO : public boost::noncopyable { ...

Goran.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-11-2010
Goran <> wrote:
> 1. understand "the rule of three" (http://en.wikipedia.org/wiki/
> Rule_of_three_%28C%2B%2B_programming%29)


Minor nitpick, but I think it's a bit incorrect to say that
"if a class defines one of the following it should probably explicitly
*define* all three" (emphasis mine).

In many cases it's sufficient to *declare* (rather than define) the
copy constructor and assignment operator (and declare them private), if
your class is such that it requires a user-defined destructor. Often this
is a much easier and less laborious way of adding the safety measure
without having to actually go through the trouble of fully implementing
copying (especially since in many cases there's no clear "best" approach
at copying an object; should it use deep-copying, lazy copying, or just
reference counting, or perhaps something else completely?)

> 2. class FO : public boost::noncopyable { ...


Is there any other advantage of boost:noncopyable other than that it
saves you from writing two lines of code in the private section of the
class? (And no, "it gives a better error message" is not a good enough
argument for requiring Boost to be installed in the system. Anybody who
is even half-competent at C++ will understand what it means if the copy
constructor or assignment operator of a class is inaccessible because it
has been declared private.)
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-11-2010
suresh wrote:

> Hi,
> Kindly consider the code segment below: I have a function object
> containing a pointer variable which is passed to min_element
> algorithm. My problem is the pointer variable is deleted twice and I
> do not know how to fix this issue.
>
> class FO{
> public:
> set<int>::size_type size(){return s->size()}
> bool operator()(int a, int b);
> ~FO();
>
> private:
> set<int> * s;
> };
>
> FO::FO(){
> s = new set<int>;
> }
>
> FO::~FO(){
> delete s;
> }
>
> bool FO:perator()(int a,int b){
> s->insert(a);
> return (a<b);
> }
>
> int main(){
> vector<int> v;
> //vector populated
> FO fo;
> min_element(v.begin(),v.end(),fo);
> }
>
> The variable 's' is defined as a pointer bcz min_element algorithm
> takes a copy of its function object argument.


I take it, you want to inspect the set after min_element() has run.
Otherwise, the above would not be a rationale.

> Now inside the
> min_element algorithm, the copy of 'fo' is deleted which results in
> freeing of the memory associated with 's'. But in the main, the
> original object fo is destructed and then also the same memory is
> freed and this gives a abort error.
>
> How to solve this kind of a problem?


Don't have FO take ownership and use pointers only to manage addresses of
objects that already exist. Something like this (off the cuff and untested):

typedef std::set< int > int_set;

class FO {
int_set * where;
public:
FO ( int_set & ref )
: where ( &ref )
{}
bool operator() ( int lhs, int rhs ) const {
where->insert(a);
return ( a < b );
}
}

int main ( void ) {
std::vector< int > v;
...
int_set inspect;
min_element( v.begin(), v.end(), FO( inspect ) );
// now inspect contains some elements
}


Best

Kai-Uwe Bux
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-11-2010
Goran wrote:

> On Sep 11, 5:34 am, suresh <suresh.amritap...@gmail.com> wrote:
>> Hi,
>> Kindly consider the code segment below: I have a function object
>> containing a pointer variable which is passed to min_element
>> algorithm. My problem is the pointer variable is deleted twice and I
>> do not know how to fix this issue.
>>
>> class FO{
>> public:
>> set<int>::size_type size(){return s->size()}
>> bool operator()(int a, int b);
>> ~FO();
>>
>> private:
>> set<int> * s;
>>
>> };
>>
>> FO::FO(){
>> s = new set<int>;
>>
>> }
>>
>> FO::~FO(){
>> delete s;
>>
>> }
>>
>> bool FO:perator()(int a,int b){
>> s->insert(a);
>> return (a<b);
>>
>> }
>>
>> int main(){
>> vector<int> v;
>> //vector populated
>> FO fo;
>> min_element(v.begin(),v.end(),fo);
>>
>> }
>>
>> The variable 's' is defined as a pointer bcz min_element algorithm
>> takes a copy of its function object argument. Now inside the
>> min_element algorithm, the copy of 'fo' is deleted which results in
>> freeing of the memory associated with 's'. But in the main, the
>> original object fo is destructed and then also the same memory is
>> freed and this gives a abort error.
>>
>> How to solve this kind of a problem?

>
>
> 1. understand "the rule of three" (http://en.wikipedia.org/wiki/
> Rule_of_three_%28C%2B%2B_programming%29)


That hint points out the problem, ...

> 2. class FO : public boost::noncopyable { ...


.... but fo is passed by value into min_element(), so would not a copy be
necessarily made?


Best

Kai-Uwe Bux
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      09-11-2010
On 11 sept, 11:48, Juha Nieminen <nos...@thanks.invalid> wrote:
>
> > 2. class FO : public boost::noncopyable { ...

>
> * Is there any other advantage of boost:noncopyable other than that it
> saves you from writing two lines of code in the private section of the
> class? (And no, "it gives a better error message" is not a good enough
> argument for requiring Boost to be installed in the system. Anybody who
> is even half-competent at C++ will understand what it means if the copy
> constructor or assignment operator of a class is inaccessible because it
> has been declared private.)


Oh that is probably too harsh. Boost is often better than current half-
baked C++0x support gradually becoming available. In fact most experts
have boost (or more likely several revisions of it) installed anyway.
The reason is usually not noncopyable, true. Several libraries in
boost may be restricted by policy (questionable quality), but i have
heard no-one restricting usage of noncopyable. It is ~40 lines of
mostly comments that does not cause any code or data with most
compilers and it certainly documents the design intentions well
enough. Only thing i am not sure is why to use "public" inheritance
from it.
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      09-13-2010
On Sep 13, 1:04*pm, Christian Hackl <ha...@sbox.tugraz.at> wrote:
> Öö Tiib ha scritto:
>
> > Only thing i am not sure is why to use "public" inheritance from it.

>
> You don't have to use public inheritance. You can just write
>
> class Example : boost::noncopyable
> {
>
> };


Yes, one of the very few classes for what specifying access adds
nothing. I was questioning about Goran's original "public" (you
snipped):

> 2. class FO : public boost::noncopyable { ...


"public" is noise, name of class looks like macro and noncopyable does
not fit to OP problem, but Juha's response felt a bit like throwing
baby away instead of dirty water.
 
Reply With Quote
 
suresh
Guest
Posts: n/a
 
      09-13-2010
On Sep 10, 9:24*pm, Garrett Hartshaw <gharts...@gmail.com> wrote:
> > Hi,
> > Kindly consider the code segment below: I have a function object
> > containing a pointer variable which is passed to min_element
> > algorithm. My problem is the pointer variable is deleted twice and I
> > do not know how to fix this issue.

>
> > class FO{
> > public:
> > set<int>::size_type size(){return s->size()}
> > bool operator()(int a, int b);
> > ~FO();

>
> > private:
> > set<int> ** s;
> > };

>
> > FO::FO(){
> > s = new set<int>;
> > }

>
> > FO::~FO(){
> > delete s;
> > }

>
> > bool FO:perator()(int a,int b){
> > s->insert(a);
> > return (a<b);
> > }

>
> > int main(){
> > vector<int> *v;
> > //vector populated
> > FO fo;
> > min_element(v.begin(),v.end(),fo);
> > }

>
> > The variable 's' is defined as a pointer bcz min_element algorithm
> > takes a copy of its function object argument. Now inside the
> > min_element algorithm, the copy of 'fo' is deleted which results in
> > freeing of the memory associated with 's'. But in the main, the
> > original object fo is destructed and then also the same memory is
> > freed and this gives a abort error.

>
> > How to solve this kind of a problem?

>
> I'm no expert, but I would say that a smart pointer (e.g.
> std::tr1::shared_pointer) would probably be your best choice.


thanks Garret. Is there any difference between boost::shared_ptr and
std::tr1::shared_pointer? I am hearing about both for the first time.
suresh

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
an error message- Object reference not set to an instance of anobject. Montezuma's Daughter ASP .Net 4 07-19-2008 05:58 PM
I.E. jscript error: 'document.getElementById(...)' is null or not anobject. Elizabeth.Lattanzio@gmail.com Javascript 3 01-03-2008 03:43 PM
Abort trap - Fatal Python error: GC object already tracked p.lavarre@ieee.org Python 1 07-10-2007 05:53 PM
Local objects in function not destructed on exception? Robbie Hatley C++ 4 09-25-2005 03:41 PM
My Hard drive just self-destructed... Terry Olsen Computer Support 4 05-15-2004 01:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57