Click here to Skip to main content
15,905,144 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Create child control Pin
zuma7714-Apr-06 4:37
zuma7714-Apr-06 4:37 
AnswerRe: Create child control Pin
Gary R. Wheeler14-Apr-06 3:26
Gary R. Wheeler14-Apr-06 3:26 
GeneralRe: Create child control Pin
zuma7714-Apr-06 4:36
zuma7714-Apr-06 4:36 
QuestionHow to simulate DoEvents() in Visual C++? Pin
WithPointer13-Apr-06 22:48
WithPointer13-Apr-06 22:48 
AnswerRe: How to simulate DoEvents() in Visual C++? Pin
nguyenvhn13-Apr-06 23:26
nguyenvhn13-Apr-06 23:26 
GeneralRe: How to simulate DoEvents() in Visual C++? Pin
WithPointer13-Apr-06 23:35
WithPointer13-Apr-06 23:35 
AnswerRe: How to simulate DoEvents() in Visual C++? Pin
Branislav13-Apr-06 23:38
Branislav13-Apr-06 23:38 
QuestionCannot throw my own exception from dll (not exactly true, but please read) Pin
toxcct13-Apr-06 22:39
toxcct13-Apr-06 22:39 
i am defining a bunch of exception classes from a dll that are supposed to be thrown from within the DLL code to the GUI which loaded the dll.
it seems that i'm having trouble with the STL std::string::c_str() function when releasing its string.
here is my code :
DLL Exceptions header :
 
<font color=blue>#ifdef</font> VCALCPARSER_EXPORTS
    <font color=blue>#define</font> VCALCPARSER_API <font color=blue>__declspec</font>(<font color=blue>dllexport</font>)
#else
    <font color=blue>#define</font> VCALCPARSER_API <font color=blue>__declspec</font>(<font color=blue>dllimport</font>)
<font color=blue>#endif</font>
 
<font color=blue>#include</font> <STRING>	<font color=green>// std::string</font>
 
<font color=blue>class</font> VCALCPARSER_API CVCalcParserException {
  <font color=blue>protected</font>:
    ExceptionNumbers    m_enExceptionNumber;
    std::string         m_strExceptionMsg;
    <font color=blue>long</font>                m_iErrorPos;
 
    CVCalcParserException(ExceptionNumbers enExceptionNumber,
                          <font color=blue>const</font> std::string& strExceptionMsg,
                          <font color=blue>int</font> iErrorPos);
 
  <font color=blue>public</font>:
    <font color=blue>virtual</font> ~CVCalcParserException();
    ExceptionNumbers    GetExceptionNumber();
    std::string         GetMessage();
    <font color=blue>long</font>                GetErrorPos();
};
 
<font color=blue>class</font> VCALCPARSER_API CSyntaxException : <font color=blue>public</font> CVCalcParserException {
  <font color=blue>protected</font>:
    CSyntaxException(ExceptionNumbers enExceptionNumber,
                     const</font> std::string& strExceptionMsg,
                     <font color=blue>int</font> iErrorPos);
 
  <font color=blue>public</font>:
    <font color=blue>virtual</font> ~CSyntaxException();
};
 
<font color=blue>class</font> VCALCPARSER_API CMathematicExpressionExpectedException : <font color=blue>public</font> CSyntaxException {
  <font color=blue>public</font>:
    CMathematicExpressionExpectedException(<font color=blue>int</font> iErrorPos);
    ~CMathematicExpressionExpectedException();
};

then, when an such an error occurs (Mathematic expression expected), i send this exception :
VALUES_TYPE CVCalcParser::Evaluate(<font color=blue>const</font> std::string& strSource)
        <font color=blue>throw</font>(CVCalcParserException) {
    <font color=blue>this</font>->ResetParserMembers(strSource);
    <font color=blue>try</font> {
        VALUES_TYPE valResult = <font color=blue>this</font>->Level_1();
        <font color=green>/*...*/</font>
    }
    <font color=blue>catch</font> (CVCalcParserException) {
        <font color=blue>throw</font>;
    }
    <font color=blue>catch</font> (...) {
        <font color=blue>throw</font> CUnknownException(0);
    }
}
 
VALUES_TYPE CVCalcParser::Level_1(<font color=blue>void</font>)
        <font color=blue>throw</font>(CVCalcParserException) {
    VALUES_TYPE valLeftOperand = <font color=blue>this</font>->Level_2();
    <font color=green>/*...*/</font>
}
 
VALUES_TYPE CVCalcParser::Level_2(<font color=blue>void</font>)
        <font color=blue>throw</font>(CVCalcParserException) {
    VALUES_TYPE valLeftOperand = <font color=blue>this</font>->Level_3();
    <font color=green>/*...*/</font>
}
 
VALUES_TYPE CVCalcParser::Level_3(<font color=blue>void</font>)
        <font color=blue>throw</font>(CVCalcParserException) {
    VALUES_TYPE valLeftOperand = <font color=blue>this</font>->Level_4();
    <font color=green>/*...*/</font>
}
 
VALUES_TYPE CVCalcParser::Level_4(<font color=blue>void</font>)
        <font color=blue>throw</font>(CVCalcParserException) {
    VALUES_TYPE valLeftOperand = <font color=blue>this</font>->Level_5();
    <font color=green>/*...*/</font>
}
 
VALUES_TYPE CVCalcParser::Level_5(<font color=blue>void</font>)
        <font color=blue>throw</font>(CVCalcParserException) {
    VALUES_TYPE valLeftOperand = <font color=blue>this</font>->Primary();
    <font color=green>/*...*/</font>
}
 
VALUES_TYPE CVCalcParser::Primary(<font color=blue>void</font>)
        <font color=blue>throw</font>(CVCalcParserException) {
    <font color=blue>this</font>->GetToken();
    <font color=blue>switch</font> (<font color=blue>this</font>->m_tokCurrentToken) {
    <font color=blue>case</font> ...:
        <font color=green>/*...*/</font>
    <font color=blue>default</font>:
        <font color=blue>throw</font> CMathematicExpressionExpectedException(<font color=blue>this</font>->m_iCurrentIndex);
    }
}

and at last, i catch this exception thrown from the dll in my GUI MFC project :
<font color=blue>void</font> CVisualCalcDlg::OnCalculate() {
    CString strSource, strDest;
    m_peInput->GetWindowText(strSource);

    <font color=green>// Initializes and calls the Parser</font>
    <font color=blue>try</font> {
        VALUES_TYPE valResult = m_Parser.Evaluate((LPCTSTR)strSource);
        <font color=green>/*...*/</font>
    }
    <font color=blue>catch</font> (CSyntaxException& ex) {
        strDest.Format(<font color=gray>"Syntax error %d : %s"</font>, ex.GetExceptionNumber(), ex.GetMessage().<code>c_str()</code>);
        m_peInput->SetSel(ex.GetErrorPos(), ex.GetErrorPos());
    }
    <font color=blue>catch</font> (...) {
        strDest.Format(<font color=gray>"Unknown parser internal error"</font>);
    }
    <font color=green>/*...*/</font>
}

when i look at the call stack, i see that the destructor of the std::string object is call at that point (it didn't leave the scope yet), and sope other functions are called down, until i get a dark message, that seems to say that i have a pointer that's been invalidated.
when i put a breakpoint on the line which contains that c_str() call and watch the ex exception object, all his datas (exception description message, position and error code) are correctly set.

i'm really stuck about that, because that behavior weren't happening when the code that i moved in the DLL was in the same project as the GUI.

any hint anybody ?
AnswerRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
Cedric Moonen14-Apr-06 4:17
Cedric Moonen14-Apr-06 4:17 
GeneralRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
toxcct14-Apr-06 4:25
toxcct14-Apr-06 4:25 
GeneralRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
Cedric Moonen14-Apr-06 4:35
Cedric Moonen14-Apr-06 4:35 
GeneralRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
toxcct14-Apr-06 4:35
toxcct14-Apr-06 4:35 
GeneralRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
Cedric Moonen14-Apr-06 4:38
Cedric Moonen14-Apr-06 4:38 
GeneralRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
toxcct14-Apr-06 4:46
toxcct14-Apr-06 4:46 
GeneralRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
Cedric Moonen14-Apr-06 4:52
Cedric Moonen14-Apr-06 4:52 
GeneralRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
toxcct15-Apr-06 1:14
toxcct15-Apr-06 1:14 
Generalat last, solvedn thanks to you ! Pin
toxcct15-Apr-06 8:33
toxcct15-Apr-06 8:33 
GeneralRe: at last, solvedn thanks to you ! Pin
Cedric Moonen16-Apr-06 1:45
Cedric Moonen16-Apr-06 1:45 
AnswerRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
Cedric Moonen14-Apr-06 4:43
Cedric Moonen14-Apr-06 4:43 
GeneralRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
toxcct14-Apr-06 4:45
toxcct14-Apr-06 4:45 
GeneralRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
Cedric Moonen14-Apr-06 4:49
Cedric Moonen14-Apr-06 4:49 
GeneralRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
Cedric Moonen14-Apr-06 4:54
Cedric Moonen14-Apr-06 4:54 
AnswerRe: Cannot throw my own exception from dll (not exactly true, but please read) Pin
Stephen Hewitt14-Apr-06 21:28
Stephen Hewitt14-Apr-06 21:28 
QuestionWhy VC++? Pin
HakunaMatada13-Apr-06 22:36
HakunaMatada13-Apr-06 22:36 
AnswerRe: Why VC++? Pin
toxcct13-Apr-06 22:42
toxcct13-Apr-06 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.