?
Solved

null pointer error rectified by non-relevant code

Posted on 2007-10-11
9
Medium Priority
?
1,804 Views
Last Modified: 2008-01-09
I am working in opencv trying to obtain image data from a new camera. For the longest time, upon execution I kept receiving the following error:

"Null pointer (The Image has a NUll data pointer) in function cvGetMat.....cxarray.cpp (2795)"
Then I pressed abort and got:
Access violation at address 5AD71531 in module UxTheme.dll read of address 00000014

To test whether I was passing numbers less than 1 to my array, I added this code:

if (i_Width<1)
                                {
                                    //MessageBox(NULL,"Less then 1","Error",MB_OK);
                                }
                                else
                                {
                                }

What confuses me is that when I added these lines, and pressed ok, then the camera images were displayed. If I went back and commented the line out, the pointer error would reoccur. My only guess is that I am adding a delay with the message box. I don't know, but even when the images are being shown it is severly delayed. Yet, if I use the demo program instead of my own GUI it blazes. Furthermore is the width is less than 1, why didn't I receive an error? What is wrong with my code and why is this bizzare (seemingly) behavior occuring?

Here is my code:

DECLARE_APP(MVMFrmApp)

                        static unsigned char* CameraImage;
                        static int i_Width;
                        static int i_Height;
                        static bool no_start_fault = true;
                            void FrameCallBack( TProcessedDataProperty* Attributes, unsigned char* Frameptr )
                                    {
                                        if ( Attributes->CameraID == 1 )
                                        {
                                         delete [] CameraImage;
                                         i_Width=Attributes->Column;
                                         i_Height=Attributes->Row;
                                         CameraImage = new unsigned char[i_Width*i_Height+512];   // <--- choose the correct size based on Attributes
                                         memcpy(CameraImage, Frameptr, i_Width*i_Height+512);
                                        }
                                }

....bunch of API calls

while( m_bLife )
                              {
                                    if( TestDestroy( ) == 1 )
                                          break;

                                if (CameraStatusInst.Start_Button_Status(false, true))
                                {

                                IplImage* NIRImage = 0;

                                if ( NIRImage == 0 )
                                {
                                // get a frame
                                int startframegrab_success = -1;
                                startframegrab_success = BUFUSB_StartFrameGrab( 1 );

                                if ( startframegrab_success< 0)
                                {

                                CameraStatusInst.Frame_Grab_Status(true, false); //set initial conditions to false

                                }
                                else
                                {
                                CameraStatusInst.Frame_Grab_Status(true, true); //
                                }

                                // create an IplImage in buffer

                                if (i_Width<1)
                                {
                                    MessageBox(NULL,"Less then 1","Error",MB_OK);
                                }
                                else
                                {
                                }



                                NIRImage = cvCreateImageHeader(cvSize(i_Width, i_Height), IPL_DEPTH_8U, 1);
                                if (NIRImage == NULL)
                                throw std::runtime_error("Could not create cv image header");
                                cvInitImageHeader(NIRImage, cvSize(i_Width, i_Height), IPL_DEPTH_8U, 1, IPL_ORIGIN_TL, 4);

                                }


                                cvSetData(NIRImage, CameraImage, i_Width * 1 ); //set image data from buffer



0
Comment
Question by:Wanderinglazyeye
  • 4
  • 3
  • 2
9 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 600 total points
ID: 20058040
When the 1st error message appears, press "Retry" instead of abort. That will take you to the code location that generates the message. Then choose the call stack and go back to the last piece of your code that you see in that call stack. The reason for that error should be around there.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 1400 total points
ID: 20058114
>>>> If I went back and commented the line out,
>>>> the pointer error would reoccur

It looks like you either writing beyond array boundaries somewhere or that you are using already freed (deleted) memory. In both cases adding a message box could change the way the heap was organised so that the error didn't show up.

If using Visual C++ you should force the debugger to stop immediately when detecting an access violation. You could do settings in Debug - Exception - Win32 exceptions - access violation.

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20058193
>>>> Access violation at address 5AD71531
>>>> in module UxTheme.dll read of address 00000014

Looks like you were using a NULL pointer to a struct/class which has a member at offset 0x14 == 20. That way the (wrong) address 0x0000014 was read.

  struct Any
  {
        int iarr[5];
        char* psz;
        ...
  };

   Any* pany = NULL;

  cout << pAny->psz << endl;  // access violation at 0x00000014


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Wanderinglazyeye
ID: 20059222
It would help if I could locate the line(s) causing the problem. (per JKR) When I press retry, it opens the program (which was run in WxDevC++) in an instance of Microsoft Studio. Then it says "MVM.exe has triggered a break point". How do I get to the problem line from here?
0
 
LVL 86

Expert Comment

by:jkr
ID: 20059243
Hm, does WxDevC++ have a debugger? If so, try running the program under that debugger.
0
 

Author Comment

by:Wanderinglazyeye
ID: 20061574
Debugging is disabled for non Ming compilers. Uggh, can't debug.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20063851
>>>> it opens the program (which was run in WxDevC++) in an instance of Microsoft Studio.
That was the VS debugger which seems to be your default debugger.

You could try to debug with it (though it depends on WxDevC had made a compatible debug database .pdb ). If your program runs for some time without crashing (if not add a message box somewhere) you should use that time, open VS and goto Debug menu.  First go into menu Debug - Exceptions - Win32 exceptions and select the exception you got (most likely it is 'access violation'). Select it and make the Debugger to stop immediately when detecting that exception (in VC8 you simply have to check the exception, in VC6 it is one of the flags you can set). After that goto Debug - Attach Process ... and attach your currently running process to the VC debugger. It mybe a problem for the debugger to find sources. You could try to help by making the source directory your current directory, e. .g by opening some of the sources in Visual Studio editor. I didn't try it with foreign sources but I mean to remember that the debugger is asking for a source path in case it didn't find any. Try to set breakpoints in a source. If it refuses to accept the breakpoint you may have to copy the .pdb file made at build time (hopefully) to the place where the executable was started in. Copy it to all places where it can be looked for. In VC executables the path of the .pdb was stored in the executable itself, so it makes less problems (normally). You migh t check this by opening the .exe with a hex editor and goto the end of the executable (or near the end) or search for pdb.

Good Luck

0
 

Author Comment

by:Wanderinglazyeye
ID: 20068213
Ok, I think I know what the problem is.We have racing conditions.  It seems that this buffered video camera has about 1/4 to 1/2 second delay (estimated) no matter what setting I choose - from 200 us underexposure to low resolution. When the program starts the image (for whatever reason) is delayed and the opencv routine tries to set the array, but the array is null, because there is a delay, thus producing the error.  Once this message is passed, it doesn't occur again until I restart the program. Therefore, the delay is the problem. Problem is, I don't know why there is a delay. In the sample program, there is no delay. I will contact the manufacturer and pose the question. In the meantime, does anyone think that this is a USB driver issue or some other factor? Could the thread itself be causing a delay? I don't see how, becuase my other vid cam used the same thread routine and blazed.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20117795
>>>> and the opencv routine tries to set the array, but the array is null
generally, crashing with a NULL pointer is always a software problem and can solved by software. Even if the camera delays for some reason, taht is no valid reasons for using a NULL pointer after.

So, you need to check how to handle racing condition and whether it is your responsibility or that of the library vendor (manufacturer). I made the experience that errors mostly occured in my responsibility and actually it is only a handful of occasions (in 26 years) where a problem only could be solved by a bug-fix of  third-party library.

>>>> void FrameCallBack( TProcessedDataProperty* Attributes, unsigned char* Frameptr )

Here in that function you are copying to Frameptr neither passing the allocated size nor checking whether it is not NULL. I don't know whether the problem occurs here but always checking pointers before use and always passing a size parameter together with a variable sized buffer, may help to make your part safer.

>>>> Null pointer (The Image has a NUll data pointer)
>>>> in function cvGetMat.....cxarray.cpp (2795)
Here, you see that the image software detected the error. The question is whether it is your code or an internal code of the software which produced the NULL pointer. As it looks that they handled their errors well, my first guess would be that you supplied a NULL pointer where a well-sized buffer was required. But it is a guess only.

Regards, Alex

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question