Solved

Unable to catch STL acess violation exceptions in VS2005

Posted on 2007-03-22
4
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

624 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