Solved

Unable to catch STL acess violation exceptions in VS2005

Posted on 2007-03-22
4
468 Views
Last Modified: 2010-07-27
Hi Guys,

I am using Visual Studio 2005 and have "Yes With SEH Exceptions (/EHa)" for project properties: C++-->Code Generation-->Enable C++ Exceptions.

But I am not able to catch the following Microsoft STL vector access violation exception error. I am able to catch normal null object pointer access violation exceptions.

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
      try
      {
            string sTmpString ;
            vector<string> vTmpTokens;
            vTmpTokens.push_back("T");
            sTmpString = vTmpTokens[1].c_str();

      }
      catch (...)
      {
            cout<<"Caught an access violation";
            return 1;
      }
}

Thank you,
Kiran
0
Comment
Question by:palliki
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18777217
You cannot catch access violations as STL exceptions, since they are SEH exceptions unles you provide a translator, e.g. like in http://msdn2.microsoft.com/en-us/library/5z4bw5h5(VS.80).aspx


#include <windows.h>
#include <eh.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
{
    cout <<  "In trans_func." << endl;
    throw std::exception;
}

int main()
{
      try
      {
            _set_se_translator( trans_func );

            string sTmpString ;
            vector<string> vTmpTokens;
            vTmpTokens.push_back("T");
            sTmpString = vTmpTokens[1].c_str();

      }
      catch (...)
      {
            cout<<"Caught an access violation";
            return 1;
      }
}
0
 
LVL 3

Accepted Solution

by:
Darrylsh earned 125 total points
ID: 18777302
use at() instead of [ ]

so instead of:

sTmpString = vTmpTokens[1].c_str();

you should use

sTmpString = vTmpTokens.at(1).c_str();
0
 

Author Comment

by:palliki
ID: 18777763
Hi Darrylsh,

Can you please explain the reason?
The operator [] and the function at() both return a reference to the vector element at a specified position. what's the difference?

Thanks a lot,
Kiran
0
 
LVL 3

Expert Comment

by:Darrylsh
ID: 18782642
The difference is that at() does bounds checking and throws an exception when you give it an invalid index. As a trade-off for speed, [] does not do any bounds checking and does not throw a c++ exception.  Instead, you get an access violation just as you would with a c-array.  

This trade-off is done by design.   I would speculate based that it was a compromise to encourage C users to adopt the c++ containers which they would probably not be inclined to do if it was significantly slower.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question