variable initialization results in "unhandled exception"

W

whiskers

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?
 
M

mlimber

whiskers said:
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
 
S

Salt_Peter

whiskers said:
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 ) {
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top