簡體   English   中英

處理異常,但在被捕獲后程序中止

[英]Exception handled, but after being caught program is aborted

我正在基於表達式模板(元編程)編寫C ++庫。

我有一個Matrix類,並且還實現了SubMatrix類來提取矩陣的一部分。 我已經為賦值具有不同操作數大小的情況設置了Assignment =運算符的異常處理,現在我為子矩陣索引與原始索引不匹配的情況設置了子矩陣提取的異常處理矩陣。 我已驗證分配=的異常處理正常工作。

SubMatrix提取的語法如下

B=SubMatrix(A,a,b,c,d);

具有以下等效的Matlab

B=A(a:b,c:d);

當我嘗試

Matrix<double>      A_H(3,4);
Matrix<double>      B_H(3,2);
try { B_H = SubMatrix(A_H,0,1,1,2); } catch(exception &e) { cout << e.what() << endl; return; } 

SubMatrix的正確異常被捕獲,但此后程序立即中止。 我已經驗證通過添加getch();殘酷地凍結視頻輸出來捕獲正確的異常getch(); try-catch

try { B_H = SubMatrix(A_H,0,1,1,2); } catch(exception &e) { cout << e.what() << endl; getch(); return; } 

有人解釋嗎? SubMatrix的異常處理與Assignment =處理之間是否存在任何“干擾”? 預先感謝您的任何幫助。

編輯-異常原因所在的功能

template<class T> 
Expr<SubMatrixExpr<const T*,T>,T> SubMatrix(const Matrix<T>&v,const int a,const int b,const int c,const int d) 
{   if((a >= 0) && (a < v.GetRows()) && (a <= b) && (b >= 0) && (b < v.GetRows()) && 
       (c >= 0) && (c < v.GetColumns()) && (c <= d) && (d >= 0) && (d < v.GetColumns())) 
    { 
        typedef SubMatrixExpr<const T*,T> SExpr; 
        return Expr<SExpr,T>(SExpr(v.GetDataPointer(),v.GetRows(),v.GetColumns(),a,b,c,d),b-a+1,d-c+1); 
    } else {    char* str0 = "************************************\n"; 
                char* str1 = "* CPU SubMatrix indices must match *\n"; 
                char* str2 = "Matrix size: "; 
                char* str3 = "SubMatrix indices (a,b,c,d): "; 
                char* catString = (char*) malloc(2*strlen(str0)+strlen(str1)+strlen(str2)+strlen(str3)+50*sizeof(char)); 
                sprintf(catString, "%s%s%s\n%s%i x %i\n%s(%i,%i,%i,%i)\n",str0,str1,str0,str2,v.GetRows(),v.GetColumns(),str3,a,b,c,d); 
                throw  GenericError(catString,__FILE__,__LINE__); 
            } 
}

編輯-例外處理類別

#define Error_msg_1 "Error in file"
#define Double_new_line "\n\n"
#define Error_msg_2 "on line"

class LibraryException: public std::exception
{
    private:
        const char *message_;
        const char *file_;
        int line_;
    protected:
        LibraryException(const char *message, const char* file, int line): message_(message), file_(file), line_(line) {}
    public:
        int get_line() const { return line_; }
        const char* get_file() const { return file_; }
        virtual const char* what() const throw() 
        {
            char buf[20];
            sprintf(buf, "%d", line_);

            char* catString = (char*) malloc(strlen(Error_msg_1)+strlen(Double_new_line)+strlen(file_)+strlen(Double_new_line)+strlen(Error_msg_2)+strlen(buf)+strlen(message_));
            sprintf(catString, "%s \n\n%s\n\n%s %s\n\n%s", Error_msg_1,file_,Error_msg_2,buf,message_);
            return catString; }
};

class GenericError: public LibraryException
{
public:
    GenericError(const char *message, const char* file, int line) :
    LibraryException(message, file, line) {}
};

編輯-MALLOC和虛擬功能

上一篇文章malloc()和虛函數到底是什么問題? ,這表明在虛擬函數中使用malloc存在一些問題。 無論如何,我也嘗試過使用new ,但是問題仍然存在,並且檢查了我無法在virtual函數中動態分配任何內容。 此外,此問題是“間歇性的”,有時會發生,有時不會。 最后,我已經解決了這樣的靜態分配問題

        virtual const char* what() const throw() 
        {
            char buf[20];
            sprintf(buf, "%d", line_);

            char catString[2048];
            sprintf(catString, "%s \n\n%s\n\n%s %s\n\n%s", Error_msg_1,file_,Error_msg_2,buf,message_);
            return &catString[0]; }
        };
char* catString = (char*) malloc(strlen(Error_msg_1)+strlen(Double_new_line)+strlen(file_)+strlen(Double_new_line)+strlen(Error_msg_2)+strlen(buf)+strlen(message_));

這里沒有分配足夠的內存,因為除了所有字符串之外,格式字符串還包含空格\\ ns和終端零。 雖然Double_new_line被計算為\\ n \\ n個空格,並且終端零仍然保留。
還有為什么使用malloc而不是new 甚至更好的std::string 由於我看不到任何免費,這將導致內存泄漏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM