ChillyMc
ChillyMc

Reputation: 173

memory corruption due to #pragma pack error - std map corruption- crashing on insert

I have a project I am working on where I have some strange behavior with the std maps.

I had my own typedef map defined which mapped strings to a pointer of a custom type. The application crashed anytime that I excess the map after I add the first pair to the map.

After a lot of messing around I changed the map to a and moved it to the first call in my application and it still crashes. I have no idea what could be going on. Any help would be appreciated.

Here is the code that crashes at the moment.

LoggerPtr syslogger(Logger::getLogger("CISInterface"));

int main(int argc, char *argv[])
{
    typedef std::map<string, string> MyMapDef;
    MyMapDef tmpString;
    tmpString.insert(MyMapDef::value_type("0000", "d"));
    tmpString.insert(MyMapDef::value_type("1111", "d")); //Crashes here.
    tmpString.insert(MyMapDef::value_type("2222", "d"));

//  std::string configFile;
//  int c;
//  if(argc < 2)
//  {
//      //Must have c option
//      std::cout << "Usage -c configFileName" << std::endl;
//      exit(EXIT_FAILURE);
//  }
//Rest of main commented out. 
...

And here is the stack trace -

CISInterface Debug [C/C++ Application]  
    gdb/mi (10/31/12 6:02 PM) (Suspended)   
        Thread [1] (Suspended: Signal 'SIGSEGV' received. Description: Segmentation fault.) 
            6 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const()  0x00000032fd49c416    
            5 std::operator< <char, std::char_traits<char>, std::allocator<char> >() basic_string.h:2317 0x0000000000417ec7 
            4 std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::operator() stl_function.h:230 0x000000000041706f  
            3 std::_Rb_tree<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::_Select1st<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::_M_insert_unique() stl_tree.h:1170 0x0000000000415d00    
            2 std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::insert() stl_map.h:500 0x00000000004150eb   
            1 main() CISInterface.cpp:29 0x000000000041916d 
gdb (10/31/12 6:02 PM)  
/home/cillian/workspace/CISInterface/Debug/CISInterface (10/31/12 6:02 PM)

What other areas should I be looking at that could be causing problems. Could it be in the libraries that I'm linking with? I have created a second project with just these lines of code that links with the same libraries (but doesn't have any code that calls into them.) and it doesn't crash.

Upvotes: 6

Views: 2098

Answers (1)

ChillyMc
ChillyMc

Reputation: 173

Problem solved.

Thought I'd add it here on the off chance anyone else ever does the same thing.

I slowly removed files in my project to try and find the offending file. I was thinking that it must be something defined in a header file that was causing issues (like a static). It took a long time but I think I've found it. I had a header file that defines a number of structs. These are serialized to the wire so I had them 1 byte aligned using #pragma pack (push) which I put at the top of the file and #pragma pack (pop) at the bottom. But I then added a couple of #include statements after the first #pragma definition meaning that these includes were aligned incorrectly and caused some nondeterministic behavior. Thanks everyone that had a look. Should probably use the attribute syntax and I wouldn't had the problem. Offending code is below for completeness.

#pragma pack (push)
#pragma pack (1)

#include <string> //Wrong place for includes!
#include <Units.h> 

typedef struct 
{ 
....
}
#pragma pack (pop) 

Thanks to everyone who had a look at initial problem.

Upvotes: 4

Related Questions