views:

254

answers:

4

Hello! I am, unfortunately, not working on a program developed by myself fully. I recently I noticed a Visual Studio Fatal Error on operator-- of unordered_set, which was called from simple insertion of a pointer to unordered_set. After reviewing the locals, I've noticed that set only has 2 elements last of which is NULL (so I suppose that's what it crashed on). Now to the question: How (theoretically) can an unordered_set (or any other STL container) get a NULL pointer as one of its elements. Program is multi-threaded, but according to my reviews, that part of code should only be accessed from one thread. Thanks.

Call stack and parts of source code for those who are interested: http://privatepaste.com/c8e7f35a4e (PushToProcessed is called from Object itself, it passes reference to itself, so cannot be NULL)

+4  A: 

a NULL is a perfectly valid pointer value. If a NULL is passed in, it will be treated just like any memory address. Since you're seeing only 2 entries I would wager a guess that what you really are seeing is many NULL pointers being "added" to your set but since it is a set only one copy retained.

Edit:

To test my hypothesis of many items being added all of NULL value why not put in a simple call to cout stating "New item added with address = " the value of the pointer. Bet you'll be surprised.

wheaties
a NULL pointer cannot be added to there, theres just no way to :o
Stranger
Without your code I can only make the assumption you've got a container of raw pointers which do allow NULL.
wheaties
Cannot be added? You mean I can't go `container.insert(0)`?
GMan
It can be added theoretically, but my pointer can never be NULL (the only one place in program I insert to that set)
Stranger
Those are famous last words: "My program can never do THAT!"
wheaties
Stranger, are your surprised that there's a `NULL` in your container, which you didn't put there?
Dmitry
yes, object puts itself into that set (by calling a function), null checks are also persistent.
Stranger
Are you using the pointers "raw" or using some kind of smart pointer wrapper?
Joe
I use raw pointers.
Stranger
Can you please post a minimal test case that reproduces it? Or at least snippets of the code that inserts and removes and checks the container?
GMan
I would add an assert() before the insert() to check the pointer definitely isn't NULL.
Bids
+5  A: 

Sets can contain pointers which can be NULL.

#include <iostream>
#include <vector>

int main () {
    std::vector<int *> foo;
    foo.push_back (NULL);
    std::cout << foo.size () << std::endl;
    return 0;
}

A container can easily contain a NULL value. Are you talking about the internals of a container getting messed up?

ezpz
+1  A: 

I believe a container pointer value can be set via a dereferenced non-const iterator as in (*iter) = NULL. This of course would be bad.

ergosys
A: 

Added source code to the paste in the main question.

Stranger