STL functions shows a segmentation fault.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: STL functions shows a segmentation fault.

  1. #1
    Join Date
    Apr 2008
    Posts
    159

    STL functions shows a segmentation fault.

    Hi,

    I wrote one application using STL API's [vector,map,make_pair,pair].
    Application is working fine in Windows 2008 server.

    Same application failed in Windows Server 2012 R2 and the failure is randomly occurred.[once in 10 iteration]
    I suspect some of my PC update is the root cause for this random failure.

    I would like to know which is latest STL version for Windows 2012 R2 server.
    Is there any latest patch/update released by Microsoft?

    Does it good idea to use Boost lib for the same?
    Which is the Boost lib version for Windows Server 2012 R2.

    There is no compilation issue on Server 2008/Server 2012 R2 server.

    Thanks in Advance
    Dave

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: STL functions shows a segmentation fault.

    Quote Originally Posted by Dave1024 View Post
    ...
    I suspect some of my PC update is the root cause for this random failure.
    But I would suspect that the "root cause for this random failure" is your own code.
    The fact you hasn't got similar problem in Windows 2008 server yet, does not mean your code is absolutely correct.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2008
    Posts
    159

    Re: STL functions shows a segmentation fault.

    Hi,

    Windows 7 is also working fine.
    Could you let me know the recent release/patch on STL,if any?
    Which is the boost lib version allowed in Windows 2012 R2 server?

    -Dave

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,145

    Re: STL functions shows a segmentation fault.

    I'm with VictorN on this one, the most likely cause is that there is a bug in your code. I've seen this kind of thing before where it works on some machines but not on others. Someone at my work had a problem like this not so long ago, and it was because they were taking an iterator to vec.begin(), applying a push_back and then dereferencing the iterator. The act of applying the push_back was causing a vector reallocation and making the iterator invalid (if you apply a push_back on a vector, you should assume that all iterators, references and pointers to elements in the vector are invalidated). On some machines running Windows the code still ran "fine", on others it blew up with an invalid memory access. The bug was there on all the machines, it just so happened that on some it was never visible and appeared to run ok, even though it technically wasn't (that is the nature of undefined behaviour).

    Are you able to post the code?

    As a final note. If there was a bug in the Microsoft STL implementation, I would expect you would find remarks all over google. Are you compiling the same piece of code on every platform, or are you compiling on one platform and running on multiple platforms?

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: STL functions shows a segmentation fault.

    Segmentation fault is basically a memory issue. This is often caused by trying to read/write to a memory location which is no longer valid - usually by using a pointer which hasn't been correctly initialised, using memory which has been 'deleted' or causing memory to be potentially re-allocated and not updating pointers to refer to the new memory. When memory is re-allocated no assurance is given that the beginning address of the re-allocated memory is the same beginning address as the old memory. It may or may not be the same. Consequently any used pointers need to be updated following such re-allocation.

    A container iterator is just a form of pointer. So any operation on a container that may cause memory re-allocation (eg a vector push-back or insert etc - see http://www.cplusplus.com/reference/stl/ for details of which functions for which containers can cause memory re-allocation) will invalidate any iterator and potentially cause problems.

    Depending upon the value of the re-allocated memory problems may not manifest themselves immediately (the start address of the new allocated memory may be the same address as the old). Depending upon what else is running on the computer and the memory manager (which may change between OS versions, SPs etc) such re-allocation problems may go undetected on one computer (where the program appears to run normally), on another cause a program crash some time after the problem has actually occurred and on another even cause a program to produce incorrect results.

    These memory allocation type issues can be very hard to find. If you are not explicitly re-allocating memory in your code then I would be examining closely code following where a pointer is set (including where iterators are set/updated) for code which may cause memory re-allocation to take place.

    Good luck
    Last edited by 2kaud; September 1st, 2014 at 04:06 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 2008
    Posts
    159

    Re: STL functions shows a segmentation fault.

    Hello 2kaud,

    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
    Does this _SECURE_SCL macro had any OS dependency? Because we used it to avoid std::vector crash with clear() and push_back() function when we build some third party library.

    Thanks
    Dave.

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: STL functions shows a segmentation fault.

    The _SECURE_SCL symbol is compiler defined - not OS defined. If used and the iterator access is unsafe then a run-time error is caused - which is what you are seeing? It is not used to avoid a vector crash but to ensure that a run-time error is always caused when use of an iterator is unsafe.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,427

    Re: STL functions shows a segmentation fault.

    Quote Originally Posted by Dave1024 View Post
    Hello 2kaud,

    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
    Does this _SECURE_SCL macro had any OS dependency? Because we used it to avoid std::vector crash with clear() and push_back() function when we build some third party library.

    Thanks
    Dave.
    If a static library is built with _SECURE_SCL=0, the all libraries that link to it that use STL must be built with the same setting.

    As to your code "working" on OS and not another machine -- since you are using C++, the last thing you should do is blame the OS, the machine, whether STL has a patch release, etc. There are some companies that would fire a C++ programmer if the first thing the programmer suspects is something other than their own code is at fault.

    The reason is simple -- unlike most other computer languages, C++ has something called undefined behaviour. This means that if your program has a bug that may corrupt memory, overwrite the boundaries of an array, return pointers or references to local variables, has uninitialized variables and pointers, fail to check error return codes or catch exceptions, etc. there is absolutely no guarantee how your program will run.

    Always suspect that it is your coding is the fault, and only if you have bonafide evidence that it is some other library or third-party code can you claim "it isn't me, it's something else".

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 2nd, 2014 at 04:51 PM.

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,886

    Re: STL functions shows a segmentation fault.

    there really is nothing in the STL that makes it OS dependant. If that were the case, it wouldn't be touted as an "easy" path to make your code more platform independant.

    if your code fails on one machine and not on another.
    and you aren't using OS specific calls, just C++ libs
    then 99.9999% chance says you wrote a bug in your code.

    yes, there is the 0.0001 chance there's a bug in the C++ libs. but it would surprise me a lot that ONLY you have an issue with it and it hasn't already been fixed.

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: STL functions shows a segmentation fault.

    Quote Originally Posted by OReubens View Post
    there really is nothing in the STL that makes it OS dependant. If that were the case, it wouldn't be touted as an "easy" path to make your code more platform independant.
    but different compilers may have different implementations whilst still adhering to the 'standard'.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center