Solved

Unable to catch STL acess violation exceptions in VS2005

Posted on 2007-03-22
4
461 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now