views:

102

answers:

2

I'm using Visual Studio 2010 Beta 2 (also tried with NetBeans), and I'm having a segmentation fault in the following code:

// One of the @link s20_3_3_comparisons comparison functors@endlink.
template <class _Tp>
struct less : public binary_function<_Tp, _Tp, bool>
{
  bool
  operator()(const _Tp& __x, const _Tp& __y) const
  { return __x < __y; }                          //this is the problem line
};

I don't know what in my program calls it, but I am trying to find out. (I think it's a map) Does anyone know what to do, or has encountered this before?

+5  A: 

There's nothing wrong with this code; the problem lies in your code that's calling it. (In fact, since this is part of the STL, it's extremely unlikely that there's a problem with this code.) It's probably getting passed an invalid reference due to deallocated memory, a NULL pointer, or similar.

less is used, by default, for std::map, std::set, and likely some other containers that I'm not thinking of right now, so you can check if you have any containers such as those that are being left with invalid values.

(Really, though, the easiest approach is to do as James McNellis said - run it in a debugger and look at your stack trace.)

Josh Kelley
+1, it is also used in different algorithms (like `sort`, *heap* adapters...)
David Rodríguez - dribeas
+2  A: 

I had the same problem yesterday.

This is tried and tested code so there is a very low probability this is causing the crash.

Usually there are three possibilities for this crash:

  • another problem is causing a corruption of the data that this function is called on
  • another problem is causing a corruption of your stack causing this function to be called when it shouldn't be
  • any combination of the two possibilities above.

To diagnose this, run your code in the VS debugger. When your application crashes, look at the parameter values and check that the stack trace shown in the debugger is the same as the stack trace you should see (click on each entry in the stack trace and look and see the code is calling what it should).

utnapistim
Thank you, this helped a lot. Turned out I wasn't passing an array correctly to a function.
noryb009