English 中文(简体)
防止C++ DLL使用内部捕获量
原标题:Prevent C++ DLL exception using try catch internally

I m developing a C++ DLL that allocate an array for the main application. The function return an error code and not the pointer to the new created array, so the address of the first member will be written in a parameter of the function. Example:

int foo(int** arrayPtr) {
  int* array = new int[10];
  *arrayPtr = array;
  return 0;
}

因此,我的主要职责是:

int* myArray;
int ret;
ret = foo(&myArray);

现在,我的阿雷拉站到了新建立的阵列。

QUESTION 1: Is there a better way to do this?

Than the more interesting question. If I pass NULL as parameter for foo, I generate an Access Violation exception because

*arrayPtr = array;

将以0x00 000字书写。

因此,我增加了一个试捕区。

int foo(int** arrayPtr) {
  int* array = new int[10];
  try {
    *arrayPtr = array;
  } catch(...) {
    return 1;
  }
  return 0;
}

我预计,当我把红 f称作准绳时,它将返回。 1. 事实并非如此。 它产生例外。

问题 为什么DL的试捕组群不工作?

感谢大家!

P.S.:在主干 t中,利用尝试捕获物直接产生同样的例外情形,会产生例外(或者更好,它正确处理的是试捕组)。

问题回答

假设你重新使用VC++,try. fishs,由于缺省例外处理模式只捕获synchronous exception和准入违规情况asynchronous。 http://msdn.microsoft.com/en-us/library/1deeycx5.aspx”rel=“nofollow”>/EH(Exception Handling Model)。

如果您改变项目环境,将使用<代码>/EHa而不是/EHsc。 之后,<代码>try. Arthur将抵挡出入。

That said, why not explicitly check for NULL? Using exceptions for flow control is bad form.

int foo(int** arrayPtr) {
    if (!arrayPtr)
        return 1;
    *arrayPtr = new int[10];
    return 0;
}
  1. 这在很大程度上是这样做的。 可以肯定的是,必须披露一项功能,删除“foo”电话所分配的记忆块(如果你干ll使用的是不同于主机)。

  2. Access violations are not supposed to throw C++ exceptions, although there is some setting in VC++ that would make a SEH exception to be mapped to a C++ one, which is generally considered a bad idea.

To question 1:
I don t see what is bad with the function. Can you define what you mean with better.
You could use std::shared_ptr with an array type or boost::shared_array to have a better resource handling. But this depends on the interface you want to use.

问题2:

try {
    *arrayPtr = array;
  } catch(...) {
    return 1;
  }

When arrayPtr is NULL this will create an access violation. You cannot catch those with c++ try/catch blocks.

  1. The other option is to return the pointer instead of a code. Returning NULL for a failed allocation seems pretty obvious.

2a. Passing NULL to your function seems more like an error on the calling side. Terminating the program with access violation in not unreasonable. That will show the caller where his error is!

页: 1 捕获条款只能满足从C++法典中提取的例外情况。 硬件的陷阱,如同对准入的侵犯,没有被抓住。

如果你不想要<条码>新,则你可以使用<条码>新(原:无行)内[10];

某些制度使你完全可以忽略不计的例外情况,因此依赖处理这些例外情况是坏的。 特别是在你能够干.的情况下。 您的<代码>foo功能应当看一看:

int foo(int** arrayPtr) 
{   
    // If a valid pointer has been passed in...
    if(arrayPtr) {
        // update the pointer to point at the allocated memory.
        *arrayPtr = new int[10];   
        return 0; 
    } 
    return 1;
}

www.un.org/Depts/DGACM/index_spanish.htm 更好的办法是参照点。 这样,不可能通过<条码>-NUL的点子,你的问题实际上就会消失。

// Pass in a reference to an array pointer.  The reference can t be NULL...
int foo(int* &arrayPtr) 
{   
    // update the reference
    arrayPtr = new int[10];   
    return 0; 
}

之后,你呼吁的法典成为:

int* myArray; 
int ret; 
ret = foo(myArray); // Pass in by reference

我认为,也值得指出的是,根据你原来的法典,即使你按照你的期望行事,你会把分配的阵列漏掉,因为你打上了:

int foo(int** arrayPtr) 
{   
    int* array = new int[10];   
    try {     
        *arrayPtr = array;   
    } 
    catch(...) 
    {     
        // EVEN IF THIS HAD WORKED AS INTENDED, YOU LEAK array BECAUSE IT S NOT DELETED
        // delete array; // is missing
        return 1;       
    }   
    return 0; 
} 




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签