473,805 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Destroying set of smart pointers segfaults

Hi,

I thought it'd be a better idea to start a new thread dealing directly
with my problem. Okay, here's again what's happening:
I'm storing boost::shared_p trS in an std::set. I supposed that on
program exit, when the set's dtor is called, the shared_ptrS would clean
up after themselves appropriately. What actually happens though is that
the program segfaults when the set is destructed.

I noticed that erasing the smart pointers from the std::set does not
work as expected. IIRC, std::set finds its elements by checking for
/equivalance/, right? That means, it would check for !less && !greater
right?
So, does that work with boost::shared_p tr at all? I know it features an
operator==, but that would only work if the container would check for
equality, which is not the case.

But then, even if my observation would be correct, I can't explain why
destroying the set would cause the program to segfault.

Any ideas?

Thanks,
Matthias
Aug 27 '05 #1
5 1711
Here are the relevant parts of my program code:

class Task {/* ... */}; // the definition is not relevant

// this class stores the set in question
class TaskManager
{
public:
void add_task(Task*) ;
void remove_task(Tas k*);
// ...
private:
std::set< boost::shared_p tr<Task> > tasks_;
// ...
};

void TaskManager::ad d_task(Task *task)
{
tasks_.insert(b oost::shared_pt r<Task>(task)) ;
// ...
task->start();
}

void TaskManager::re move_task(Task *task)
{
task->stop();
// ...
tasks_.erase(bo ost::shared_ptr <Task>(task)) ; // this does in fact
NOT erase anything
}

Tasks are added by calling TaskManager::ad d_task():

// in some other class:
taskmanager.add _task(new Task(...));

Hope that helps.
- Matthias
Aug 27 '05 #2
Matthias Kaeppler wrote:

tasks_.erase(bo ost::shared_ptr <Task>(task)) ;*//*this*does*in*f act


You can't legally initialize a new shared_ptr with a given pointer, if
there are other shared_ptr's around pointing to the same object. You
probably could, if an intrusive reference counting technique were used,
which is not the case.

You should probably handle every 'task' object using a shared_ptr as it
gets instantiated, and change TaskManager's interface so that it accepts
shared_ptr arguments.

Max
Aug 27 '05 #3
"Matthias Kaeppler" <no*****@finite state.org> wrote in message
news:de******** *****@news.t-online.com...
[...]
tasks_.erase(bo ost::shared_ptr <Task>(task)) ; // this does in fact
NOT erase anything
}

[...]

As it shouldn't. From the docs:

------
under the equivalence relation defined by operator<, !(a < b) && !(b < a),
two shared_ptr instances are equivalent if and only if they share ownership
or are both empty.
------

The shared_ptr in the set container doesn't share ownership with the
shared_ptr that you just created (they are unaware of each other's
existence), so they are not considered equivalent.

--
David Hilsee
Aug 27 '05 #4
Matthias Kaeppler wrote:
Here are the relevant parts of my program code:

class Task {/* ... */}; // the definition is not relevant

// this class stores the set in question
class TaskManager
{
public:
void add_task(Task*) ;
void remove_task(Tas k*);
// ...
private:
std::set< boost::shared_p tr<Task> > tasks_;
// ...
};


This is getting you in trouble. You can't mix raw pointers and
shared_ptr. If you're going to use a shared_ptr to control a resource
you must initialize a shared_ptr object with the pointer to the resource
and then abandon the pointer. (It's not quite that drastic, but it's a
good rule of thumb when you're getting started):

shared_ptr<Task > sp(new Task);

And now your TaskManager's interface should traffic in shared_ptr<Task >s
rather than Task*'s.
--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 27 '05 #5
Okay, thanks for all your input.

One more question:
When passing around a shared_ptr, is it better practice to do it by
reference or by value?

e.g.:
void foo(const shared_ptr<Bar> & ptr);
or
void foo(shared_ptr< Bar> ptr);

I mean, constructing a copy is certainly not that expensive I guess, but
if it's reference counted, it would imply a useless increment and
decrement of the reference counter right?
Right now, I pass it by reference to const, just to be safe, although
this looks kind of weird for a "pointer" (I usually try to keep my code
intuitive, and this would imply to pass it by value just like a raw
pointer).

Regards,
Matthias
Aug 28 '05 #6

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

Similar topics

9
2157
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own code or other people's code you have come across would be appreciated. I am also trying to identify the likelihood nad frequency of scenarios where smart pointer solutions would not be appropriate, i.e. for some reason such as performance or...
27
3414
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined as ff: typedef struct { float *data ; int count ;
5
2576
by: Matthias Kaeppler | last post by:
Hi, I was wondering, since STL containers are based around copying, whether it's a good idea to use reference counted smart pointers, such as boost::shared_ptr in STL containers. I can't store the objects directly in a container, because they must not be duplicated, so I have to use pointers. I'm just not certain about using raw pointers or some kind of smart pointer. Regards,
4
2859
by: Matthias Kaeppler | last post by:
Hi, I'm having a hard time figuring out how I can initialize a smart pointer based on a certain condition: if something then ptr = 0; // init with NULL else ptr = new XYZ; // init with a sane value endif
8
5158
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good documentation like the following: http://axter.com/smartptr Now I'm on a client site, and I'm trying to create the same type of documentation on a very large project. I ran the Doxygen program, and it ran for over 16 hours, before I had
92
5139
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
33
5088
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
4
2709
by: Olumide | last post by:
Hello - I have two classes A and B as follows: class B{ public: ~B(){ cout << "destroying B" << endl; } }; class A{
54
12034
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously concerned that there is a shift to raw pointers. We are not developing system software but rather...
0
9596
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
10604
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
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10103
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
9179
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
7644
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
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4316
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

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.