Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

stl

crash

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.

like image 268
ChillyMc Avatar asked Oct 31 '12 07:10

ChillyMc


People also ask

Can memories be corrupted?

While memory plays an important role in our lives, it is not infallible. Memory can easily be distorted, corrupted, or otherwise altered, whether intentionally or unintentionally.

How do you fix memory corruption?

You simply need to buy compatible memory, checking with a free tool such as Crucial's System Scanner (see Resources). Once you have this, you can simply unclip the faulty memory module, clip the new one in place, replace your computer's case and start it up as normal.

What is malloc (): memory corruption?

Writing to memory which you have not allocated is undefined behaviour. That's because malloc() returns a section of memory which you may write to, so when you write past the end of that region, you are overwriting something which is not yours. That could be a structure used by malloc itself, or something else entirely.

What is memory leak and memory corruption?

Memory leak = did not release the memory that a pointer is currently. pointing to, and the pointer goes out of scope. Corruption = write to a memory location that was not intended to.


1 Answers

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.

like image 169
ChillyMc Avatar answered Oct 03 '22 05:10

ChillyMc