variable initialization results in "unhandled exception"

Discussion in 'C++' started by whiskers, Nov 8, 2006.

  1. whiskers

    whiskers Guest

    I'm debugging some code and I have to admit that I don't know yet how
    it works.
    But I ran into a problem I can't explain
    The program is a DLL that retrieves raw data from a camera, builds
    histograms based on the pixel values, and displays them on the screen;
    here's a small excerpt:

    VOID CHistogramContainer::appendFrameData(WORD *pdwRawData, int nWidth,
    int nHeight)
    {
    int nPixelPos;
    __int64 total;
    __int64 median;
    double accuVar;


    for (UINT i = m_nVStart; i <= m_nVEnd; i++) {
    for (UINT j = m_nHStart; j <= m_nHEnd; j++) {
    // Becareful! pixelval is value of pixel and
    // index for distribution as well
    int pixelval = *(pdwRawData+((i-1)*nWidth+j));


    The last line always returns an unhandled exception. When I try to see
    what value (i-1)*nWidth+j is equal to, it's always the same somewhere
    along the lines of -809601 or something (and nWdith always = 1920).
    First of all, i starts at 0, so I don't think i-1 is even a valid value
    here.

    Anyway, what baffles me is that if I create a new variable such as int
    pixel = 0; I get an unhandled exception!! If I initialize accuVar to a
    value, the initialization does not work!

    What is causing this strange behavior, is it a memory issue? How is it
    possible to get get an unhandled exception when initializing a
    variable? I really know little of how this DLL works, but the trivial
    task of adding a variable shouldn't be so bizarre, should it?
    whiskers, Nov 8, 2006
    #1
    1. Advertising

  2. whiskers

    mlimber Guest

    whiskers wrote:
    > I'm debugging some code and I have to admit that I don't know yet how
    > it works.
    > But I ran into a problem I can't explain
    > The program is a DLL that retrieves raw data from a camera, builds
    > histograms based on the pixel values, and displays them on the screen;
    > here's a small excerpt:
    >
    > VOID CHistogramContainer::appendFrameData(WORD *pdwRawData, int nWidth,
    > int nHeight)
    > {
    > int nPixelPos;
    > __int64 total;
    > __int64 median;
    > double accuVar;
    >
    >
    > for (UINT i = m_nVStart; i <= m_nVEnd; i++) {
    > for (UINT j = m_nHStart; j <= m_nHEnd; j++) {
    > // Becareful! pixelval is value of pixel and
    > // index for distribution as well
    > int pixelval = *(pdwRawData+((i-1)*nWidth+j));
    >
    >
    > The last line always returns an unhandled exception. When I try to see
    > what value (i-1)*nWidth+j is equal to, it's always the same somewhere
    > along the lines of -809601 or something (and nWdith always = 1920).
    > First of all, i starts at 0, so I don't think i-1 is even a valid value
    > here.
    >
    > Anyway, what baffles me is that if I create a new variable such as int
    > pixel = 0; I get an unhandled exception!! If I initialize accuVar to a
    > value, the initialization does not work!
    >
    > What is causing this strange behavior, is it a memory issue? How is it
    > possible to get get an unhandled exception when initializing a
    > variable? I really know little of how this DLL works, but the trivial
    > task of adding a variable shouldn't be so bizarre, should it?


    It's probably the result of writing beyond your array bounds here or
    somewhere else. Doing that results in what the Standard calls undefined
    behavior, which can mean anything from nothing to random bad things to
    your computer synthesizing a new kind of cheese from dust particles it
    has collected. It's really undefined.

    The unhandled exception you're talking about is what Microsoft throws
    sometimes when you try to access something outside your memory space.
    You can catch it with catch(...) or with their __try/__except/__finally
    extensions.

    Cheers! --M
    mlimber, Nov 8, 2006
    #2
    1. Advertising

  3. whiskers

    Salt_Peter Guest

    whiskers wrote:
    > I'm debugging some code and I have to admit that I don't know yet how
    > it works.
    > But I ran into a problem I can't explain
    > The program is a DLL that retrieves raw data from a camera, builds
    > histograms based on the pixel values, and displays them on the screen;
    > here's a small excerpt:
    >
    > VOID CHistogramContainer::appendFrameData(WORD *pdwRawData, int nWidth,
    > int nHeight)
    > {
    > int nPixelPos;
    > __int64 total;
    > __int64 median;
    > double accuVar;
    >
    >
    > for (UINT i = m_nVStart; i <= m_nVEnd; i++) {
    > for (UINT j = m_nHStart; j <= m_nHEnd; j++) {


    I have no idea what your CHistogramContainer looks like nor what
    m_nVEnd is, but typically the "end" of a sequence is one past the last
    element. Therefore:

    for ( UINT i = m_nVStart; i < m_nVEnd; ++i ) {
    for ( UINT j = m_nHStart; j < m_nHEnd; ++j ) {


    > // Becareful! pixelval is value of pixel and
    > // index for distribution as well
    > int pixelval = *(pdwRawData+((i-1)*nWidth+j));
    >
    >
    > The last line always returns an unhandled exception. When I try to see
    > what value (i-1)*nWidth+j is equal to, it's always the same somewhere
    > along the lines of -809601 or something (and nWdith always = 1920).
    > First of all, i starts at 0, so I don't think i-1 is even a valid value
    > here.
    >
    > Anyway, what baffles me is that if I create a new variable such as int
    > pixel = 0; I get an unhandled exception!! If I initialize accuVar to a
    > value, the initialization does not work!
    >
    > What is causing this strange behavior, is it a memory issue? How is it
    > possible to get get an unhandled exception when initializing a
    > variable? I really know little of how this DLL works, but the trivial
    > task of adding a variable shouldn't be so bizarre, should it?
    Salt_Peter, Nov 8, 2006
    #3
    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. Amit
    Replies:
    8
    Views:
    3,799
    Alan Pretre
    Aug 4, 2003
  2. Pardhasaradhy
    Replies:
    1
    Views:
    529
    Natty Gur
    Jul 9, 2003
  3. Lee Simpson
    Replies:
    0
    Views:
    1,574
    Lee Simpson
    Jul 18, 2003
  4. Ola
    Replies:
    0
    Views:
    532
  5. Tedka
    Replies:
    2
    Views:
    2,672
    Mr. Dot Net
    Jul 19, 2004
Loading...

Share This Page