Exception handling C++ and Qt

Discussion in 'C++' started by Ady, Feb 8, 2011.

  1. Ady

    Ady Guest

    Hi,

    I'm not sure if this is the place to ask this so please bear with me just in
    case you are able to help.

    I am writing an application using Nokias Qt Creator on Windows and C++
    (because it's free) which is intended to examine the contents of a data file
    byte-by-byte and generating an output of byte, short int and long int values
    depending on the position of what the user has selected. All this works.
    However, when I try to add a QDateTime variable to the program and I move
    the relevant data segment to the variable, then try to use the .isValid()
    function I always get an access violation which Qt can't handle.

    I know I'm doing something daft here, and I'm quite new to C++. If anyone
    can point me in the right direction it would be appreciated.

    I am posting the relevant section of code.

    void Widget::examinedata()
    { //let the user select a starting byte and the dump the contents //of
    a byte, word and longword. The text is already displayed char *ptr;
    unsigned char byteval; short wordval; unsigned short uwordval; long
    longval; unsigned long ulongval; QDateTime dt; QString sDate;
    ptr = hexdump->pointerval() + buffer; //buffer pointer is calculated when
    the user memcpy(&byteval,ptr,1); //releases the mouse
    button wordval = 0; uwordval = 0; longval = 0; ulongval = 0; //get
    word and longword values so long as we're not past the end of the buffer
    if ((long)hexdump->pointerval() <= (long)(fsize - sizeof(wordval)))
    { memcpy(&wordval,ptr,sizeof(wordval));
    memcpy(&uwordval,ptr,sizeof(uwordval)); } if
    ((long)hexdump->pointerval() <= (long)(fsize - sizeof(longval)))
    { memcpy(&longval,ptr,sizeof(longval));
    memcpy(&ulongval,ptr,sizeof(ulongval)); } if
    ((long)hexdump->pointerval() <= (long)(fsize - sizeof(dt))) {
    memcpy(&dt,ptr,sizeof(dt)); } sDate = "invalid"; try { if
    (dt.isValid()) sDate = dt.toString("dd/MM/yyyy hh:mm"); }
    catch(...) { sDate = "Error!!"; } //output the results
    lbldata->setText(QString("<html>Char: %1<br><br>") .arg(byteval) +
    QString("sWord: %1<br>") .arg(wordval) + QString("uWord:
    %1<br><br>") .arg(uwordval) + QString("sLong: %1<br>")
    ..arg(longval) + QString("uLong: %1") .arg(ulongval) +
    QString("sDate: %1") .arg(sDate) +
    QString("</html>") );}
     
    Ady, Feb 8, 2011
    #1
    1. Advertising

  2. Ady

    Ady Guest

    APOLOGY for code formatting error

    Sorry about the format - if anyone wants the code I'll resend it

    Ady
     
    Ady, Feb 8, 2011
    #2
    1. Advertising

  3. Ady

    ptyxs Guest

    Re: APOLOGY for code formatting error

    On Feb 8, 5:44 pm, "Ady" <> wrote:
    > Sorry about the format - if anyone wants the code I'll resend it
    >
    > Ady


    Nobody will want the code until it is readable... so better to resend
    it anyway
     
    ptyxs, Feb 8, 2011
    #3
  4. Ady

    Ady Guest

    Let's try this again...

    void Widget::examinedata()
    {
    //let the user select a starting byte and the dump the contents
    //of a byte, word and longword. The text is already displayed
    char *ptr;
    unsigned char byteval;
    short wordval;
    unsigned short uwordval;
    long longval;
    unsigned long ulongval;
    QDateTime dt;
    QString sDate;

    ptr = hexdump->pointerval() + buffer; //buffer pointer is calculated
    when the user
    memcpy(&byteval,ptr,1); //releases the mouse button

    wordval = 0; uwordval = 0;
    longval = 0; ulongval = 0;

    //get word and longword values so long as we're not past the end of the
    buffer
    if ((long)hexdump->pointerval() <= (long)(fsize - sizeof(wordval)))
    {
    memcpy(&wordval,ptr,sizeof(wordval));
    memcpy(&uwordval,ptr,sizeof(uwordval));
    }
    if ((long)hexdump->pointerval() <= (long)(fsize - sizeof(longval)))
    {
    memcpy(&longval,ptr,sizeof(longval));
    memcpy(&ulongval,ptr,sizeof(ulongval));
    }
    if ((long)hexdump->pointerval() <= (long)(fsize - sizeof(dt)))
    {
    memcpy(&dt,ptr,sizeof(dt));
    }
    sDate = "invalid";
    try
    {
    qDebug() << "before isValid";
    if (dt.isValid())
    sDate = dt.toString("dd/MM/yyyy hh:mm");
    qDebug() << "after isValid";
    }
    catch(...)
    {
    sDate = "Error!!";
    }
    //output the results
    lbldata->setText(QString("<html>Char: %1<br><br>") .arg(byteval) +
    QString("sWord: %1<br>") .arg(wordval) +
    QString("uWord: %1<br><br>") .arg(uwordval) +
    QString("sLong: %1<br>") .arg(longval) +
    QString("uLong: %1") .arg(ulongval) +
    QString("sDate: %1") .arg(sDate) +
    QString("</html>")
    );
    }
     
    Ady, Feb 8, 2011
    #4
  5. Ady <> wrote:
    > I'm not sure if this is the place to ask this so please bear with me just in
    > case you are able to help.


    > I am writing an application using Nokias Qt Creator on Windows and C++
    > (because it's free) which is intended to examine the contents of a data file
    > byte-by-byte and generating an output of byte, short int and long int values
    > depending on the position of what the user has selected. All this works.
    > However, when I try to add a QDateTime variable to the program and I move
    > the relevant data segment to the variable, then try to use the .isValid()
    > function I always get an access violation which Qt can't handle.


    > I know I'm doing something daft here, and I'm quite new to C++. If anyone
    > can point me in the right direction it would be appreciated.


    > I am posting the relevant section of code.


    It's, even when reformated, nearly impossible to understand what the
    code is meant to do - too many important variables are not defined
    etc. But I noticed that you do something here that looks rather
    fishy:

    QDateTime dt;
    ....
    memcpy(&dt,ptr,sizeof(dt));

    While already reading binary POD data from a file is proble-
    matic (unless they got written by the same type of machine)
    writing out a whole class instance and then trying to read
    it back in again is a nice recipe for disaster. It might
    work for very simple classes (non-polymorphic, no virtual
    functions, only POD member data etc. and then only if you
    use the same architecture and compiler and, maybe even the
    same compiler version), for more complicated classes it
    won't. Serialize the data of the class before writing them
    to the file. Or at least convert the date to some integer
    type (e.g. the number of seconds since some fixed point in
    time) and write that out. That's still horrible and will
    bite you if you should ever try to read in the file on a
    different architecture than where the file was written on,
    but it's at least a tiny bit saner;-)

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Feb 8, 2011
    #5
  6. Ady

    Richard Guest

    [Please do not mail me a copy of your followup]

    "Ady" <> spake the secret code
    <M8f4p.64616$2> thusly:

    > QDateTime dt;


    > if ((long)hexdump->pointerval() <= (long)(fsize - sizeof(dt)))
    > {
    > memcpy(&dt,ptr,sizeof(dt));
    > }


    memcpy on classes is undefined behavior. memcpy is only defined for
    POD types.

    You'll have to extract the data some other way.
    --
    "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
    <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

    Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
     
    Richard, Feb 8, 2011
    #6
  7. On 8 fév, 17:37, "Ady" <> wrote:
    [snip]
    > I am writing an application using Nokias Qt Creator on Windows and C++
    > (because it's free) which is intended to examine the contents of a data file
    > byte-by-byte and generating an output of byte, short int and long int values
    > depending on the position of what the user has selected. All this works.
    > However, when I try to add a QDateTime variable to the program and I move
    > the relevant data segment to the variable, then try to use the .isValid()
    > function I always get an access violation which Qt can't handle.
    >
    > I know I'm doing something daft here, and I'm quite new to C++. If anyone
    > can point me in the right direction it would be appreciated.


    No it is not. serialization is actually quite common.

    > I am posting the relevant section of code.

    [snip: code]

    The read code is not sufficient, you should also post how you wrote
    the file.

    Quickly reading the Qt documentation, I notice there is a QDataStream
    class used for (de)serializing data. May be you can use it for writing/
    reading your file.

    --
    Michael
     
    Michael Doubez, Feb 9, 2011
    #7
  8. Ady

    Ady Guest

    "Michael Doubez" <> wrote in message
    news:...
    On 8 fév, 17:37, "Ady" <> wrote:
    [snip]
    > I am writing an application using Nokias Qt Creator on Windows and C++
    > (because it's free) which is intended to examine the contents of a data
    > file
    > byte-by-byte and generating an output of byte, short int and long int
    > values
    > depending on the position of what the user has selected. All this works.
    > However, when I try to add a QDateTime variable to the program and I move
    > the relevant data segment to the variable, then try to use the .isValid()
    > function I always get an access violation which Qt can't handle.
    >
    > I know I'm doing something daft here, and I'm quite new to C++. If anyone
    > can point me in the right direction it would be appreciated.


    No it is not. serialization is actually quite common.

    > I am posting the relevant section of code.

    [snip: code]

    The read code is not sufficient, you should also post how you wrote
    the file.

    Quickly reading the Qt documentation, I notice there is a QDataStream
    class used for (de)serializing data. May be you can use it for writing/
    reading your file.

    --
    Michael


    Michael,

    I feel some further explanation is required here. My program was originally
    intended to emulate the DUMP command from the OpenVMS operating system (my
    day job) on a Windows box. That bit of the program works so I decided to
    extend it allow the user to examine the contents of the file - ANY file. It
    may be a formatted file, it might not, it's whatever the user decides to
    look into. It's a tool I've found useful over the years.

    I'm using this as a tutorial for myself in both C++ and the Qt environment.
    I am used to 'legacy' languages on VMS like C, Cobol, Basic, Fortran, Pascal
    and Assembler but I'm relatively new to Windows languages.

    In answer to your post, there is therefore no writing of the original file.
    The file is selected by the user, opened, read into an appropriately sized
    buffer (malloc'd) and closed. The data is displayed in a hex dump window and
    a Ascii window (if a printable character). The user can then click or
    double-click on any byte displayed in the hex dump and the values of the
    various simple data types are displayed. I was hoping to add a date type but
    this is where I'm getting the access violations.

    If there is a simple way of translating a 64-bit date 'type' under Qt I
    would really like to see it. It doesn't seem to have anything like the
    SYSTEMTIME and FILETIME structures that are available under VC++ 6.

    Regards,

    Ady
     
    Ady, Feb 9, 2011
    #8
  9. On Feb 8, 12:36 pm, (Richard) wrote:
    > memcpy on classes is undefined behavior.  memcpy is only defined for
    > POD types.
    >
    > You'll have to extract the data some other way.


    A type defined with the "class" keyword can be POD. "class" and
    "struct" differ solely in the default visibility of members and the
    default "visibility type" of inheritance.

    I agree that writing to a non-POD type with memcpy is undefined
    behavior. Don't do it.
     
    Joshua Maurice, Feb 9, 2011
    #9
  10. Ady

    Richard Guest

    [Please do not mail me a copy of your followup]

    Joshua Maurice <> spake the secret code
    <> thusly:

    >On Feb 8, 12:36 pm, (Richard) wrote:
    >> memcpy on classes is undefined behavior.  memcpy is only defined for
    >> POD types.
    >>
    >> You'll have to extract the data some other way.

    >
    >A type defined with the "class" keyword can be POD.


    QDateTime is not a POD.
    --
    "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
    <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

    Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
     
    Richard, Feb 9, 2011
    #10
  11. On 9 fév, 17:32, "Ady" <> wrote:
    > "Michael Doubez" <> wrote in message
    > >
    > > news:....
    > > On 8 fév, 17:37, "Ady" <> wrote:
    > > [snip]
    > >
    > > > I am writing an application using Nokias Qt Creator on Windows and C++
    > > > (because it's free) which is intended to examine the contents of a data
    > > > file
    > > > byte-by-byte and generating an output of byte, short int and long int
    > > > values
    > > > depending on the position of what the user has selected. All this works.
    > > > However, when I try to add a QDateTime variable to the program and I move
    > > > the relevant data segment to the variable, then try to use the .isValid()
    > > > function I always get an access violation which Qt can't handle.

    > >
    > > > I know I'm doing something daft here, and I'm quite new to C++. If anyone
    > > > can point me in the right direction it would be appreciated.

    > >
    > > No it is not. serialization is actually quite common.
    > >
    > > > I am posting the relevant section of code.

    > >
    > > [snip: code]
    > >
    > > The read code is not  sufficient, you should also post how you wrote
    > > the file.
    > >
    > > Quickly reading the Qt documentation, I notice there is a QDataStream
    > > class used for (de)serializing data. May be you can use it for writing/
    > > > reading your file.

    > >

    >
    > I feel some further explanation is required here. My program was originally
    > intended to emulate the DUMP command from the OpenVMS operating system (my
    > day job) on a Windows box. That bit of the program works so I decided to
    > extend it allow the user to examine the contents of the file - ANY file. It
    > may be a formatted file, it might not, it's whatever the user decides to
    > look into. It's a tool I've found useful over the years.
    >

    [snip]
    >
    > In answer to your post, there is therefore no writing of the original file.
    > The file is selected by the user, opened, read into an appropriately sized
    > buffer (malloc'd) and closed. The data is displayed in a hex dump window and
    > a Ascii window (if a printable character). The user can then click or
    > double-click on any byte displayed in the hex dump and the values of the
    > various simple data types are displayed. I was hoping to add a date type but
    > this is where I'm getting the access violations.


    IIRC you have a binary dump of your memory.

    > If there is a simple way of translating a 64-bit date 'type' under Qt I
    > would really like to see it. It doesn't seem to have anything like the
    > SYSTEMTIME and FILETIME structures that are available under VC++ 6.


    SYSTEMTIME and FILETIME are C structure (POD in C++). As Richard
    pointed out, QDateTime is not POD. Looking into QT headers, it seems
    QtDate Time is composed of a QDate and a QTime but QDate, at least,
    has virtual functions. This means the layout of the class may include
    additional (RTTI) data and/or member layout reorganisation.

    In short, the internal joined data is 64 bits (depending on compiler's
    paddings) but there should also be a vtable pointer somewhere causing
    the access violation you get; this is also plateform specific.

    A solution could be to interpret your 64 bits into two unsigned int
    and use them to initialise a QDateTime structure.

    --
    Michael
     
    Michael Doubez, Feb 10, 2011
    #11
  12. Ady

    Ady Guest

    Michael,

    Thanks for the pointers, I'll look into it.

    Ade
     
    Ady, Feb 10, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    8,806
    charlie
    Mar 16, 2005
  2. Replies:
    4
    Views:
    344
    Peter
    Jun 20, 2006
  3. Mark Tarver
    Replies:
    22
    Views:
    1,375
    J Kenneth King
    Apr 26, 2009
  4. Peter
    Replies:
    34
    Views:
    2,017
    James Kanze
    Oct 17, 2009
  5. VSK
    Replies:
    0
    Views:
    267
Loading...

Share This Page