std::bad_alloc at memory location 0x0016ef74..

Pages: 12



I am trying to create covariance matrix with 12 feature images. Below is my code:
Feature() function will returns the 12 feature images.
However, when the ArC = 4, the code at->
"temp[ArC] = cvCreateImage(cvGetSize(SingleF ),SingleF->depth,SingleF->nChannels);"

show up the error mentioned below.

I am curious about the error, and I tried many method to change the coding but still faces the same issues.
Please help me.. :(

//---------------------------------------------------------------------

IplImage** ArrCov (IplImage* in, int patch_size, CvRect window,IplImage Gab0,IplImage Gab85,IplImage Gab180,IplImage Gab265 ){

int block_start_x, block_start_y;
int block_width = patch_size, block_height = patch_size;
CvMat vector_block;
int startc = 0;

CvSize sz = cvGetSize( in );
IplImage **ArrayCov = new IplImage*[40]; //Declare Covariance matrix array
IplImage* avgImage = cvCreateImage( sz, in->depth, 1); //Declare average image
IplImage* SingleF= cvCreateImage( sz, in->depth, 1);
cvZero(SingleF);


IplImage **ARCO = Feature(in,Gab0,Gab85,Gab180, Gab265); //get total Features
//showImage(ARCO[4], 0, "bubu");



for (block_start_y = window.y; block_start_y <= window.y + window.height - block_height; block_start_y += (block_height/2))
{
for (block_start_x = window.x; block_start_x<= window.x + window.width - block_width; block_start_x += (block_width/2))
{

IplImage **temp= new IplImage*[12];

for(int ArC=0; ArC<12; ArC++){
/* get properties, needed to create ROI image */





cvCopy(SingleF , ARCO[ArC], NULL);
cvSetImageROI( SingleF , cvRect(block_start_x, block_start_y, block_width, block_height));
temp[ArC] = cvCreateImage(cvGetSize(SingleF ),SingleF->depth,SingleF->nChannels);
cvCopy(SingleF, temp[ArC], NULL);
cvResetImageROI(SingleF);
cvZero(SingleF);
}

cvCalcCovarMatrix((const CvArr**)temp, 12, ArrayCov[startc], avgImage, CV_COVAR_NORMAL); //calculate covmatrix
cvReleaseImage(temp);
startc++;
}
}
cvReleaseImage(&avgImage);
cvReleaseImage(ARCO);


return (ArrayCov);
}

//---------------------------------


First-chance exception at 0x7485c41f in covF.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0016ef74..
First-chance exception at 0x7485c41f in covF.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0016e714..
First-chance exception at 0x7485c41f in covF.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
Unhandled exception at 0x76ed15de in covF.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0016e714..
You should put your code into CODE <> format tag. This will make your source easier to read and more people will ready to help you.
Last edited on
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/features2d/features2d.hpp"
#include <stdio.h>
#include <stdlib.h>
#include "cv.h"
#include "cvaux.h"
#include "cxcore.h"
#include "GraphUtils.h"
#include <math.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function
#include <cstring>
#include "covF.h"
#include "opencv2/core/core.hpp"
#include "opencv2/ml/ml.hpp"

using std::cerr;
using std::endl;
using namespace cv;
using namespace std;
using std::ofstream;



IplImage** Feature(IplImage* in,IplImage Gab0,IplImage Gab85,IplImage Gab180,IplImage Gab265 )
{
CvSize sz = cvGetSize( in );

IplImage **covarInterim = new IplImage*[12];


//IplImage* imgX = cvCreateImage( sz, in->depth,1 );
//IplImage* imgY = cvCreateImage( sz, in->depth,1 );

IplImage* xsobel = cvCreateImage(sz, IPL_DEPTH_16S,1);
IplImage* ysobel = cvCreateImage(sz, IPL_DEPTH_16S,1);
IplImage* b = cvCreateImage( sz, in->depth,1 );
IplImage* r = cvCreateImage(sz, in->depth,1 );
IplImage* g = cvCreateImage( sz, in->depth,1 );



IplImage* imgX = cvCreateImage( sz, in->depth,1 );
IplImage* imgY = cvCreateImage( sz, in->depth,1 );
//Gradient orientation
float temp_gradient;
const float PI = 3.14159265f;
//IplImage* O = cvCreateImage( sz, IPL_DEPTH_32F,1 );
IplImage* O = cvCreateImage( sz, 8,1 );
cvSetZero(O);

cvSplit(in,b,g,r,0); //R,G,B channel image



IplImage* img_gray = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U,1);
cvCvtColor(in,img_gray, CV_RGB2GRAY);
cvSobel( img_gray, xsobel, 1,0, 3 ); //gradient in X direction
cvSobel( img_gray, ysobel, 0, 1, 3 ); //gradient in Y direction
IplImage* xsobel_dest = cvCreateImage(sz,in->depth,1);
IplImage* ysobel_dest = cvCreateImage(sz, in->depth,1);
cvConvertScaleAbs(xsobel,xsobel_dest, 1, 0);
cvConvertScaleAbs(ysobel,ysobel_dest, 1, 0);
cvReleaseImage(&xsobel);
cvReleaseImage(&ysobel);
cvReleaseImage(&xsobel);
cvReleaseImage(&img_gray);


for (int y = 0; y <sz.height; y++) {

/* ptr1 and ptr2 point to beginning of the current row in the xsobel and ysobel images respectively. ptrs point to the beginning of the current rows in the O images */
float* ptr1 = (float*) (xsobel_dest->imageData + y * (xsobel_dest->widthStep));
float* ptr2 = (float*) (ysobel_dest->imageData + y * (ysobel_dest->widthStep));
float* ptrs = (float*) (O->imageData + y * (O->widthStep));


/*For every pixel in a row gradient orientation and magnitude are calculated and corresponding values set for the bin images. */
for (int x = 0; x <sz.width; x++) {

/* if the xsobel derivative is zero for a pixel, a small value is added to it, to avoid division by zero. atan returns values in radians, which on being converted to degrees, correspond to values between -90 and 90 degrees. 90 is added to each orientation, to shift the orientation values range from {-90-90} to {0-180}. This is just a matter of convention. {-90-90} values can also be used for the calculation. */

if (ptr1[x] == 0){
//temp_gradient = (float)(((atan(ptr2[x] / (ptr1[x] + 0.00001)) *(180/ PI))) + 90.0000);
temp_gradient = ((atan(ptr2[x] /(ptr1[x] + 0.00001))) * (180/ PI)) + 90;
}
else{
//temp_gradient = (float)(((atan(ptr2[x] / ptr1[x])) *(180 / PI)) + 90.0000);
temp_gradient = ((atan(ptr2[x] / ptr1[x])) * (180 / PI)) + 90;
}
//temp_magnitude = sqrt((ptr1[x] * ptr1[x]) + (ptr2[x] * ptr2[x]));
ptrs[x] = temp_gradient;
}
}

for (int i = 0; i<in->height;i++ ){
for (int j = 0; j<in->width;j++ ){
CV_IMAGE_ELEM( imgX, int, i,j )= j; //spatial layout of X
CV_IMAGE_ELEM( imgY, int, i,j )= i; //spatial layout of Y
}
}




//Assign Feature
covarInterim[0]= imgX;
covarInterim[1]= imgY;
covarInterim[2]= r;
covarInterim[3]= g;
covarInterim[4]= b;
covarInterim[5]= xsobel_dest;
covarInterim[6]= ysobel_dest;
covarInterim[7]= O;
covarInterim[8]= &Gab0;
covarInterim[9]= &Gab85;
covarInterim[10]= &Gab180;
covarInterim[11]= &Gab265;
return covarInterim;
}




IplImage** ArrCov (IplImage* in, int patch_size, CvRect window,IplImage Gab0,IplImage Gab85,IplImage Gab180,IplImage Gab265 ){

int block_start_x, block_start_y;
int block_width = patch_size, block_height = patch_size;
CvMat vector_block;
int startc = 0;

CvSize sz = cvGetSize( in );
IplImage **ArrayCov = new IplImage*[40]; //Declare Covariance matrix array
IplImage* avgImage = cvCreateImage( sz, in->depth, 1); //Declare average image
IplImage* SingleF= cvCreateImage( sz, in->depth, 1);
cvZero(SingleF);


IplImage **ARCO = Feature(in,Gab0,Gab85,Gab180, Gab265); //get total Features
//showImage(ARCO[4], 0, "bubu");



for (block_start_y = window.y; block_start_y <= window.y + window.height - block_height; block_start_y += (block_height/2))
{
for (block_start_x = window.x; block_start_x<= window.x + window.width - block_width; block_start_x += (block_width/2))
{

IplImage **temp= new IplImage*[12];

for(int ArC=0; ArC<12; ArC++){
/* get properties, needed to create ROI image */





cvCopy(ARCO[ArC],SingleF);
cvSetImageROI( SingleF , cvRect(block_start_x, block_start_y, block_width, block_height));
temp[ArC] = cvCreateImage(cvGetSize(SingleF ),SingleF->depth,SingleF->nChannels);
cvCopy(SingleF, temp[ArC], NULL);
cvResetImageROI(SingleF);
cvZero(SingleF);
}

cvCalcCovarMatrix((const CvArr**)temp, 12, ArrayCov[startc], avgImage, CV_COVAR_NORMAL); //calculate covmatrix
cvReleaseImage(temp);
startc++;
}
}
cvReleaseImage(&avgImage);
cvReleaseImage(ARCO);


return (ArrayCov);
}



//return covarInterim;}
I have posted the Function 'Feature' too..
You forgot to put your code into <> format tag again.... :(
Sorry, I am just new to this. I thought is already in the format you told me, i paste the code and i press the <> symbol beside.

Could you teach me how to make it into <> format tag? Sorry again.
You probably have a memory leak somewhere, and you run out of memory... until the program realizes and throws an std::bad_alloc.

http://stackoverflow.com/questions/5661302/do-i-need-a-cvreleaseimage-if-image-is-returned

etarion wrote:
If you're using C++, don't use IplImage*, use cv::Mat, which does memory management automatically for you.
Last edited on
WHEN EDITING, See the format menu on the right, you'll see <>. Click '<>', you'll get two tags. Put your code between two tags, for Ex <code> your code </code>... And now your code is ready!!!

Good luck!!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/features2d/features2d.hpp"
#include <stdio.h>
#include <stdlib.h>
#include "cv.h"
#include "cvaux.h"
#include "cxcore.h"
#include "GraphUtils.h"
#include <math.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function
#include <cstring>
#include "covF.h"
#include "opencv2/core/core.hpp"
#include "opencv2/ml/ml.hpp"

using std::cerr;
using std::endl;
using namespace cv;
using namespace std;
using std::ofstream;



IplImage** Feature(IplImage* in,IplImage Gab0,IplImage Gab85,IplImage Gab180,IplImage Gab265 )
{   
	CvSize sz = cvGetSize( in );

IplImage **covarInterim = new IplImage*[12];


//IplImage* imgX = cvCreateImage( sz, in->depth,1 );
//IplImage* imgY = cvCreateImage( sz, in->depth,1 );

IplImage* xsobel = cvCreateImage(sz, IPL_DEPTH_16S,1);
IplImage* ysobel = cvCreateImage(sz, IPL_DEPTH_16S,1);
IplImage* b = cvCreateImage( sz, in->depth,1 ); 
IplImage* r = cvCreateImage(sz, in->depth,1 );
IplImage* g = cvCreateImage( sz, in->depth,1 );



IplImage* imgX = cvCreateImage( sz, in->depth,1 );
IplImage* imgY = cvCreateImage( sz, in->depth,1 );
//Gradient orientation
float temp_gradient;
const float PI = 3.14159265f;
//IplImage* O = cvCreateImage( sz, IPL_DEPTH_32F,1 );
IplImage* O = cvCreateImage( sz, 8,1 );
cvSetZero(O);

cvSplit(in,b,g,r,0);      //R,G,B channel image



IplImage* img_gray = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U,1);
cvCvtColor(in,img_gray, CV_RGB2GRAY); 
cvSobel( img_gray, xsobel, 1,0, 3 ); //gradient in X direction
cvSobel( img_gray, ysobel, 0, 1, 3 );  //gradient in Y direction
IplImage* xsobel_dest = cvCreateImage(sz,in->depth,1);
IplImage* ysobel_dest = cvCreateImage(sz, in->depth,1);
cvConvertScaleAbs(xsobel,xsobel_dest, 1, 0);
cvConvertScaleAbs(ysobel,ysobel_dest, 1, 0);
cvReleaseImage(&xsobel);
cvReleaseImage(&ysobel);
cvReleaseImage(&xsobel);
cvReleaseImage(&img_gray);


for (int y = 0; y <sz.height; y++) {

/* ptr1 and ptr2 point to beginning of the current row in the xsobel and ysobel images respectively. ptrs point to the beginning of the current rows in the O images */
float* ptr1 = (float*) (xsobel_dest->imageData + y * (xsobel_dest->widthStep));
float* ptr2 = (float*) (ysobel_dest->imageData + y * (ysobel_dest->widthStep));
float* ptrs = (float*) (O->imageData + y * (O->widthStep));


/*For every pixel in a row gradient orientation and magnitude are calculated and corresponding values set for the bin images. */
for (int x = 0; x <sz.width; x++) {

/* if the xsobel derivative is zero for a pixel, a small value is added to it, to avoid division by zero. atan returns values in radians, which on being converted to degrees, correspond to values between -90 and 90 degrees. 90 is added to each orientation, to shift the orientation values range from {-90-90} to {0-180}. This is just a matter of convention. {-90-90} values can also be used for the calculation. */

if (ptr1[x] == 0){
//temp_gradient = (float)(((atan(ptr2[x] / (ptr1[x] + 0.00001)) *(180/ PI))) + 90.0000);
temp_gradient = ((atan(ptr2[x] /(ptr1[x] + 0.00001))) * (180/ PI)) + 90;
}
else{
//temp_gradient = (float)(((atan(ptr2[x] / ptr1[x])) *(180 / PI)) + 90.0000);
	temp_gradient = ((atan(ptr2[x] / ptr1[x])) * (180 / PI)) + 90;
}
//temp_magnitude = sqrt((ptr1[x] * ptr1[x]) + (ptr2[x] * ptr2[x]));
ptrs[x] = temp_gradient;
}
}

for (int i = 0; i<in->height;i++ ){
	for (int j = 0; j<in->width;j++ ){
	CV_IMAGE_ELEM( imgX, int, i,j )= j;   //spatial layout of X
    CV_IMAGE_ELEM( imgY, int, i,j )= i;   //spatial layout of Y
}
}




//Assign Feature  
covarInterim[0]= imgX;
covarInterim[1]= imgY;
covarInterim[2]= r;
covarInterim[3]= g;
covarInterim[4]= b;
covarInterim[5]= xsobel_dest;
covarInterim[6]= ysobel_dest;
covarInterim[7]= O;
covarInterim[8]= &Gab0;
covarInterim[9]= &Gab85;
covarInterim[10]= &Gab180;
covarInterim[11]= &Gab265;
return covarInterim;
}




IplImage** ArrCov (IplImage* in, int patch_size, CvRect window,IplImage Gab0,IplImage Gab85,IplImage Gab180,IplImage Gab265  ){

int block_start_x, block_start_y;
int block_width = patch_size, block_height = patch_size;
CvMat vector_block;
int startc = 0;

CvSize sz = cvGetSize( in );
IplImage **ArrayCov =  new IplImage*[40]; //Declare Covariance matrix array
IplImage* avgImage = cvCreateImage( sz, in->depth, 1);  //Declare average image 
IplImage* SingleF= cvCreateImage( sz, in->depth, 1); 
cvZero(SingleF);


IplImage **ARCO = Feature(in,Gab0,Gab85,Gab180, Gab265); //get total Features 
//showImage(ARCO[4], 0, "bubu");



for (block_start_y = window.y; block_start_y <= window.y + window.height - block_height; block_start_y += (block_height/2))
{
for (block_start_x = window.x; block_start_x<= window.x + window.width - block_width; block_start_x += (block_width/2))
{
	
    IplImage **temp= new IplImage*[12]; 

for(int ArC=0; ArC<12; ArC++){
    /* get properties, needed to create ROI image */

    cvCopy(ARCO[ArC],SingleF);
	cvSetImageROI( SingleF , cvRect(block_start_x, block_start_y, block_width, block_height));
	temp[ArC] = cvCreateImage(cvGetSize(SingleF ),SingleF->depth,SingleF->nChannels);
	cvCopy(SingleF, temp[ArC], NULL);
	cvResetImageROI(SingleF);
	cvZero(SingleF);
	}

	cvCalcCovarMatrix((const CvArr**)temp, 12, ArrayCov[startc], avgImage, CV_COVAR_NORMAL); //calculate covmatrix
	cvReleaseImage(temp);
     startc++;
}
}
cvReleaseImage(&avgImage);
cvReleaseImage(ARCO);


return (ArrayCov);
}



//return covarInterim;} 
In case you missed my previous post, maybe you should use cv::Mat instead of cv::IplImage? (I have no experience in OpenCV so I don't know.)

Otherwise you'll have to dig and find where you're leaking memory i.e. fail to delete[] something you new[]'d.

Edit: ... or find if there's somewhere where you request too much memory by mistake...
Last edited on
Have you put two sources into <> tag?
Last edited on
Thank you Catfish2, I will try to change it into Mat.
I just wonder where is the memory leak as I have already make sure the images are already released.. :(
Horrible? If you know where my mistakes are please tell me? I am still new to this and willing to learn from mistakes.
Of course, I could be wrong about the memory leak.
Is there somehow you can check if you're not trying to use more memory than you have?
Is there any programs can be used to check whether I have use exceeded memory?
Hot Topic ???
Last edited on
Hot Topic?
Is there any programs can be used to check whether I have use exceeded memory?

The task manager, maybe, if it's smart enough to show memory usage?

If you're running Linux, there's a tool named valgrind that can check for memory leaks in the executable (check your distro's repositories) and pmap which should be standard issue.

For Windows, there's Dr. Memory and UMDH.
http://code.google.com/p/drmemory/
http://support.microsoft.com/kb/268343

Also, ignore Jackson.
Last edited on
Thank you Catfish2. I will try it out. ^^
Pages: 12