Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9249199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:07:59+00:00 2026-06-18T10:07:59+00:00

I’m tracking a bug and I run into very strange behaviour. I have set

  • 0

I’m tracking a bug and I run into very strange behaviour. I have set of pointers and when I erase them one-by-one, first one erases, but erasing another gives me segfault.
I use

   size_type erase( const key_type& key );

so it can’t be something with iterators. My debuger shows me that in callstack:

0 - std::less<cSubscriber *>::operator() //cSubscriber is an abstract base class and I have a set of cSubscriber *

1 - std::_Rb_tree<cSubscriber*, cSubscriber*, std::_Identity<cSubscriber*>, std::less<cSubscriber*>, std::allocator<cSubscriber*> >::equal_range

2 -  std::_Rb_tree<cSubscriber*, cSubscriber*, std::_Identity<cSubscriber*>, std::less<cSubscriber*>, std::allocator<cSubscriber*> >::erase

3 - std::set<cSubscriber*, std::less<cSubscriber*>, std::allocator<cSubscriber*> >::erase

4 - cEventSystem::unsubscribe //my function, it is as follows in the class which has the set as its member

cEventSystem::unsubscribe(cSubscriber * ptr)
{
   set.erase(ptr);
}

And in the base cSubscriber abstract class there is virtual destructor:

  virtual ~cSubscriber()
  {
      eventSystem.unsubscribe(this);
  }

Any ideas? I have no idea how can it cause segfault, the erase should just return 0 when there isn’t such an element. Or maybe it crashesh when trying to erase something from empty container? (I have another bug when after adding 3 differents pointers the size of set is only 2 but that’s another story).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:08:00+00:00Added an answer on June 18, 2026 at 10:08 am

    If you pass a invalid address to your std::set<SOMETHING*>::erase() it will segfault when trying to compare the passed value with what is in the container.

    For example:

    struct IntPtrComparer {
        bool operator()(int* a, int* b) const {
            return *a < *b;
        }
    };
    
    std::set<int*,IntPtrComparer> a;
    a.insert(new int);
    a.erase(NULL);
    

    Update

    based on comments

    Since you are not redefining the default comparator, and the default comparator does not dereference your pointers, then the only way is that your std::set is corrupted.

    Internally, std::set is implemented as a binary tree. This mean that it has a lot of pointers in the way to find a value and to erase it. If std::set is corrupted some of theses pointers will point to a invalid memory address. This invalid memory address will be used to pass a reference (reference of a pointer, cSubscriber* & const) of the compared value to std::less. std::less receives a reference to a pointer and is going to dereference the reference to get the pointer value.

    This way, a invalid memory inside the std::set did only show up at std::less, because the std::set did not actually touch the invalid memory, it gave the invalid memory address to our poor fella std::less that opened it and got a segfault in it’s face.

    My point with all that is that if you create a comparator that uses copies instead of reference, the corruption will show up inside the std::set when it tries to copy the pointer value to give to the comparator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.