为什么在OpenCV中访问此矩阵时会出现内存错误?

时间:2015-11-20 10:45:32

标签: c++ opencv memory-leaks valgrind

我只是想写一个给定大小的矩阵。当我在Valgrind运行这个程序时,我得到了内存错误,如下所示:

main.cpp中:

public class MainActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

TabLayout tabLayout;

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Toolbar toolbar =(Toolbar) findViewById(R.id.mainActivityBar);
     setSupportActionBar(toolbar);

    tabLayout = (TabLayout) findViewById(R.id.tabs);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
     mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setupWithViewPager(mViewPager);
}

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        switch (position) {
            case 0:
              return Item1fragment.newInstance();
            case 1:

                return Item2fragment.newInstance();
            case 2:
                return Item3fragment.newInstance();

        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return "Item1;
            case 1:
              return "Item2";
            case 2:
               return "Item3";
        }
        return "";
    }
 }

}

编写说明:

#include <iostream>
#include <opencv2/opencv.hpp>

int main()
{
    cv::Mat m = cv::Mat::zeros(cv::Size(59, 9), CV_32SC1);
    m.at<int>(9, 4) = 1;
}

最后运行Valgrind:

g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary  main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching

它在我的机器上返回此消息:

valgrind ./binary

这些是我的机器的规格:

==98408== Invalid write of size 4
==98408==    at 0x1000017F8: main (main.cpp:7)
==98408==  Address 0x10dd202cc is 4 bytes after a block of size 2,152 alloc'd
==98408==    at 0x100009EAB: malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==98408==    by 0x10001D1E6: cv::fastMalloc(unsigned long) (in /usr/local/Cellar/opencv/2.4.12/lib/libopencv_core.2.4.12.dylib)
==98408==    by 0x1000F4C77: cv::Mat::create(int, int const*, int) (in /usr/local/Cellar/opencv/2.4.12/lib/libopencv_core.2.4.12.dylib)
==98408==    by 0x1000F0A51: cv::MatOp_Initializer::assign(cv::MatExpr const&, cv::Mat&, int) const (in /usr/local/Cellar/opencv/2.4.12/lib/libopencv_core.2.4.12.dylib)
==98408==    by 0x1000018FB: cv::MatExpr::operator cv::Mat() const (mat.hpp:1227)
==98408==    by 0x1000017BC: main (main.cpp:6)

2 个答案:

答案 0 :(得分:2)

您正在访问超出范围的矩阵。

cv::Mat m = cv::Mat::zeros(cv::Size(59, 9), CV_32SC1);

将创建一个9x59矩阵(9行,59列)的矩阵。你正在访问第10行。

答案 1 :(得分:2)

您似乎混淆了矩阵的尺寸。构造一个包含59列和9行的矩阵,并访问第10行和第4列:

cv::Size(width,height); // size specification
m.at<int>(y,x); // access

因此第9行超出范围。交换尺寸或指数!