• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

pointer code not right?

I am compiling fine but getting an exception at this point in my code:

CvMat *theMatrix = FxLUTInst.getMat();
        int n_rows = theMatrix->rows;             //here

FxLUTInst getMat() is an instance of class FxLUT and should return the matrix lut_mat. I am trying to access lut_mat array elements in a matrix created elsewhere in the program, but I think I have my pointer language wrong. I think this is probably simple...

Thanks
WLE.
0
Wanderinglazyeye
Asked:
Wanderinglazyeye
4 Solutions
 
HuyBDCommented:
try

CvMat *theMatrix = (FxLUT *)FxLUTInst.getMat();
        int n_rows = theMatrix->rows;             //here

HuyBD
0
 
efnCommented:
It may be simple, but we need more information to figure out what is going on.  Is the "exception" a run-time exception or a compile error?  What does it say?  What is the declaration of CvMat?

For what it's worth, I found a declaration of CvMat on http://opencvlibrary.sourceforge.net/CxCore that looked like it just plain wouldn't work in C++.
0
 
Infinity08Commented:
It seems like your getMat() returns a NULL pointer.

From this other thread of yours :

http://www.experts-exchange.com/Programming/Languages/CPP/Q_22730694.html

Did you remove this line from the WriteLUT function ? :

        CvMat* lut_mat;

???

If that's not the problem, then can you show the code for the FxLUTInst class and its methods ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Deepu AbrahamR & D Engineering ManagerCommented:
Can you show us what id FxLUTInst.getMat(); function doing?

it will be handy to have a check like this: atleast it will not throw exceptions

int n_rows = 0;
CvMat *theMatrix=NULL;
theMatrix = (FxLUT *)FxLUTInst.getMat();
if(theMatrix!=NULL)
n_rows = theMatrix->rows;

Best Regards,
DeepuAbrahamK



 
Best Regards,
DeepuAbrahamK
0
 
WanderinglazyeyeAuthor Commented:
Hi all,

-It is a runtime exception.
-I had removed the line CvMat* lut_mat; from the WriteLUT function. The matrix was being written to correctly in that part.
-Infinity08 may correct about passing NULL pointer

---------
LUT.h
--------
class FxLUT
{

private :
   CvMat *lut_mat;



public:
    FxLUT();
    ~FxLUT();

    bool WriteLUT();
    CvMat *getMat() { return lut_mat; }

};
---------
LUT.cpp (this works fine, checked the matrix values with Infinity08's help, thread above)
----------
FxLUT::FxLUT()
{
}

FxLUT::~FxLUT()
{
}

//function to create lookup table

bool FxLUT::WriteLUT()
{
CvMat* lut_mat    = cvCreateMat(1,256,CV_8UC1);
int m_cols          = lut_mat->cols;
int m_rows          = lut_mat->rows;
int *data           = lut_mat->data.i;

            for (int i = 0; i < m_rows; ++i)
        {
            for (int j = 0; j < m_cols; ++j)
                {
                    if ((j>105)&&(j<117))   //fill ranges
                        {
                            cvSetReal2D(lut_mat, i, j, j); // Set M(i,j)
                        }
                    else
                        {
                            cvSetReal2D(lut_mat, i, j, 255); // Set M(i,j)
                        }
                }
        }

        int n_rows = lut_mat->rows;
        int n_cols = lut_mat->cols;
        int *data2 = lut_mat->data.i;
---------------
Thread.cpp:
---------------
extern FxLUT FxLUTInst;

CvMat *theMatrix = FxLUTInst.getMat();
        // use the matrix ...
        n_rows = theMatrix->rows; //RUN-TIME EXCEPTION this line

 DeepuAbrahamK's code generated this error:
198 C:\...\OpenCVThread1.cpp [Error C2440] '=' : cannot convert from 'FxLUT *' to 'CvMat *'

Thanks everybody -
WLE
0
 
Infinity08Commented:
>> -I had removed the line CvMat* lut_mat; from the WriteLUT function.

It's still there though :

        CvMat* lut_mat    = cvCreateMat(1,256,CV_8UC1);

You're still writing to a local copy instead of to the class member. Use this line instead :

        lut_mat    = cvCreateMat(1,256,CV_8UC1);


btw, you do call the WriteLUT() method before trying to access it from your thread, don't you ?
0
 
WanderinglazyeyeAuthor Commented:
Yes, I call it before starting the thread. The code changes you suggested fixed the problem. I ran a check on matrix values from the thread, and the values are fine. We know what the error was, but can you briefly explain (if not too onerous an explanation) what the problem was?

Many thanks,

WLE
0
 
Infinity08Commented:
>> We know what the error was, but can you briefly explain (if not too onerous an explanation) what the problem was?

Ok, if you havea class that has a
0
 
Infinity08Commented:
Let me try again, without clicking submit too early ... :)

>> We know what the error was, but can you briefly explain (if not too onerous an explanation) what the problem was?

Objects have data members. Those reflect the current state of the object. In your case, there is one data member called lut_mat that contains the matrix :

        class FxLUT {
            private :
                CvMat *lut_mat;
            // etc.
        };

If you create an instance of the FxLUT class, then it will also automatically contain a lut_mat matrix.

When you want to fill the matrix, you need to fill the member matrix. What you did, was create a local matrix (local to the function) in the WriteLUT function. In other words, that matrix was lost when the WriteLUT function ended.
With the change I suggested, you're filling up the member matrix, which will stay for the lifetime of the object.

For more information on classes, check this tutorial :

        http://www.cplusplus.com/doc/tutorial/classes.html
        http://www.cplusplus.com/doc/tutorial/classes2.html

It's an interesting read, and will make things a lot clearer for you !


Btw, you probably should remove the matrix in the class destructor ...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now