map.empty() giving anomalous segfault

Hi,

I'm trying to set up some code which uses a map to store data but I've been completely confounded by a segfault.

Here's the outline:

In code.cc, I #include code.hh. code.hh declares, as private,

std::map<G4int,G4VEMDataSet*,std::less<G4int> > dataMap;

where G4int and G4VEmDataSet are both working datatypes, properly included and everything.

In code.cc, I then have a function

1
2
3
4
5
6
7
void code::Clear()
{
if(!dataMap.empty())
{
  ...
}
}


I've used couts to verify that the segfault occurs at if(!dataMap.empty()) but this makes no sense to me because I would expect a compiler error if the map didn't exist or something, and if it exists, how can calling a built-in function give a segfault?

Also, I have #include <map> at the top of both my header and code files, and using namespace std at the top of both as well.

I hope I included everything relevant to the problem (this is all the places where the map is dealt with) but at least theoretically should this work?

Thanks very much
Might have something to do with G4VEMDataSet* you're inserting. If you're deleting them at the wrong time you might get a segfault. If you do delete them, try removing the delete.
I don't think G4VEMDataSet* is the problem because I'm having another segfault (after I comment out all the dataMapNRF stuff).

So I have, in my header file (private)

G4DataVector activeZNRF;

and then later (without doing anything to activeZNRF)

size_t nZ = activeZNRF.size();

Again the code compiles without error but I get a segfault here. So is there something systematically wrong with the way I am declaring objects? (The G4DataVector class itself is fine)

Thanks again
captainclogs wrote:
The G4DataVector class itself is fine


Famous last words.
Haha yes well I didn't make the G4DataVector class, have never touched it, and it works perfectly everywhere else so I would be very surprised if it were responsible for the problem.
Without source code it really is difficult to do anything other than speculate.

If your whole object goes out of scope or is released from the free store with delete then every member will be subject to seg-faults no matter how innocuous the instructions performed with them.

I suspect your problem is part of the bigger picture. Showing us perfectly good code and telling us that the other parts are perfectly good too is unlikely to solve this one.
the empty() function seems like a benign function to me which implies that you probably have undefined behavior somewhere else in the program. With undefined behavior occurring the crash or segfault may not occur where the problem actually is. Try to analyze your code and then build a small program that does something similar and attempt to duplicate the problem with something that you can post here. During the process of doing that you may discover the source of the problem on your own.
Topic archived. No new replies allowed.