混合c ++异常处理和SEH(窗口) [英] intermixing c++ exception handling and SEH (windows)

查看:194
本文介绍了混合c ++异常处理和SEH(窗口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,我调用 getaddrinfo()获取一个 sockaddr * 哪个目标内存被分配由系统。
许多人知道,你需要调用$ code freeaddrinfo()来释放由getaddrinfo()分配的内存。



现在,在我的函数中,有几个地方,我可能会抛出一个异常,因为一些函数失败。
我的第一个解决方案是将 freeaddrinfo()并入每个if-block。
但是对我而言看起来很丑,因为在函数返回之前我不得不打电话给我,所以我想出了SEH的try-finally ...



但我遇到的问题是,不允许将throw语句编入__try-block



然后,我在msdn上阅读并尝试将throw语句交换到从__try-block ...中调用的帮助函数中,而且,编译器不再呻吟了...





代码:

  void function()
{
// ...
addrinfo * pFinal;
__try
{
getaddrinfo(...,& pFinal);

// if(DoSomething1()== FAILED)
// throw(exception); //错误C2712:在需要对象展开的函数中不能使用__try

//但是这个工程
Helper();


// ...

}
__finally
{
freeaddrinfo();
}
}


void Helper()
{
throw(Exception);
}

编辑:



尝试以下,它可以抛出一个整数,但不是当我使用一个类作为一个例外:

  class X 
{
public:
X(){};
〜X(){};
};


void Helper()
{
throw(X());
}


void base()
{
__try
{
std :: cout<< 输入__try\\\
;

Helper();

std :: cout<<< 离开__try\\\
;
}
__finally
{
std :: cout<<< 在__finally\\\
;
}
};


int _tmain(int argc,_TCHAR * argv [])
{
try
{
base();
}
catch(int& X)
{
std :: cout<< 抓到X<的std :: ENDL;
}

std :: cin.get();
return 0;
}

为什么? :/

解决方案

您可以将addrinfo包装在调用 getaddrinfo 在构造函数中和 freeaddrinfo 中的析构函数。



这样它总是被释放,无论是否抛出异常。


I have a function in which I call getaddrinfo() to get an sockaddr* which targets memory is allocated by the system. As many may know, you need to call freeaddrinfo() to free the memory allocated by getaddrinfo().

Now, in my function, there are a few places, where I may throw an exception, because some function failed. My first solution was to incorporate the freeaddrinfo() into every if-block. But that did look ugly for me, because I would have had to call it anyways before my function returns, so I came up with SEH`s try-finally...

But the problem I encountered is, that it is not allowed to code the throw-statements into the __try-block

Then, I read on the msdn and tried to swap the throw-statements into the helper function called from within the __try-block... and voila, the compiler didn´t moan it anymore...

Why is that? And is this safe? This does not make sense to me :/

Code:

void function()
{
    //...
    addrinfo* pFinal;
    __try
    {
        getaddrinfo(..., &pFinal);

        //if(DoSomething1() == FAILED)
        //  throw(exception);           //error C2712: Cannot use __try in functions that require object unwinding

        //but this works
        Helper();


        //...

    }
    __finally
    {
        freeaddrinfo();
    }
}


void Helper()
{
    throw(Exception);
}

EDIT:

tried the following and it works with throwing an integer, but does not when i use a class as an exception:

class X
{
public:
    X(){};
    ~X(){};
};


void Helper()
{
    throw(X());
}


void base()
{
    __try
        {
            std::cout << "entering __try\n";

            Helper();

            std::cout << "leaving __try\n";
        }
        __finally
        {
            std::cout << "in __finally\n";
        }
};


int _tmain(int argc, _TCHAR* argv[])
{
    try
    {
        base();
    }
    catch(int& X)
    {
        std::cout << "caught a X" << std::endl;
    }

    std::cin.get();
    return 0;
}

Why? :/

解决方案

You could wrap the addrinfo in a class that calls getaddrinfo in the constructor and freeaddrinfo in its destructor.

That way it will always be freed, whether there is an exception thrown or not.

这篇关于混合c ++异常处理和SEH(窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆