PDA

Click to See Complete Forum and Search --> : New and Delete


Destined Soul
May 29th, 2002, 11:44 AM
Hi all.

I've been having a little trouble with a dll, written in C++, interfacing with VB. Well, it's not the interfacing with VB that's the problem, but rather some strange memory errors occuring.

Oh, for the record, I'm using Visual Studio 6, SP5 on Win2000 Pro.

If any of you folks are familiar with the Hough transform, or variants of it, this might make more sense, but the idea should be seen from anyone. In this dll function, I am passed an array, and create my own 2d "matrix" to copy/edit/paste the data that was sent. What I'm wondering is if the following syntax is correct:
myFunc(byte* data, short width, short height)
{
int** array;
int i, j;

array = new int* [width];
for (i = 0; i < width; i++)
array[i] = new int [height];

...

for (i = 0; i < width; i++)
delete array[i];
delete array;
}I'm wondering if there's anything wrong with this syntax, other than checking to see if any of the values are NULL.

I'm asking this mainly because I was getting the weird errorDebug Error!

Program: C:\Code\vbTest.exe

Damage: before normal block (#xxxx) at 0xYYYYYYYY

(Press Retry to debug the application)Strangely enough, I was able to track the error down to occuring when I called the delete array[i]; line (and only on some, seemingly random, values of i)

What is strange is how I fixed it. I found two seperate options that seemed to work. The first was simply changing the compiler configuration from debug to release version, and it got rid of the error.

The second fix, and even stranger, involves only the code that manipulated the array. To quickly cap what I'm doing, I'm taking an image array (grayscale, in bytes) that has already been applied a smoothen and edge-detection (thresholded to is/isNot edge) routines, and this one "marks" the possible locations of a center pixel for each edge pixel given.

The "marking" is simply taking a possible center coordinate, say (x,y), and incrementing that value of the array (ie: array[x][y]++) I found that if I keep the number of ++ operations done to the pixels to a minimum, this also got rid of the error. What I removed was two more lines of code that did:
array[x][y-1]++;
array[x][y+1]++;

Has anyone ever experience this before, or know what may be wrong with my code?

Thanx in advance, as always.

Destined

parksie
May 29th, 2002, 11:47 AM
Use delete[] array or delete[] array[i]. Just because the error goes away in Release mode doesn't mean that it isn't there anymore.

Destined Soul
May 29th, 2002, 12:37 PM
Originally posted by parksie
Use delete[] array or delete[] array[i]. Just because the error goes away in Release mode doesn't mean that it isn't there anymore. So would this be okay:for (i = 0; i < width; i++)
delete[] array[i];
delete[] array;I've never really played around with the [] brackets after delete. Is it used whenever the pointer points to more than one data element? ie:int* i = new int;
delete i;

int* j = new int [3];
delete[] j;Again, thanx.

Destined

parksie
May 29th, 2002, 12:40 PM
You got it.

The array delete and new operators are separate to the normal ones, since they need to do some magic (i.e. know how many items they allocated) and this can get seriously broken through mismatching them.

But it is as simple as that. :)

Destined Soul
May 29th, 2002, 01:18 PM
Oi.. just when I think I've gotten it fixed, this program deflates my tires.

I changed the release version back to Debug, and adjusted the deletion of the arrays exactly as I put it in my last post. However, it's still doing it! *sigh*

I put messageboxes in my code, and it always complains about the debug error when I get to the delete[] array[i]; statement.

I'm wondering, is there something else that I could be doing that could possibly cause this error? I'm going to look over my code and see exactly what I do to the matrix in hopes that I find something.

Destined

Destined Soul
May 29th, 2002, 01:35 PM
Oh good grief. I think I may have fixed this. You gotta love C++ and being able to go out of bounds of the arrays and still write data. :D It seems to work, now that I've fixed the bounds error.

Thanx, though, Parksie, for the info on the delete opertor. :)

Destined

parksie
May 29th, 2002, 01:42 PM
Originally posted by Destined Soul
You gotta love C++ and being able to go out of bounds of the arrays and still write data. :DBy-product of C.

How d'you think the Internet Worm worked? (as in the original one, many years ago) :D It overwrote past the end of a buffer and injected its own program code in.

CornedBee
May 31st, 2002, 03:04 AM
Only debug builds have guarding buffers against mem overflow.

delete[] calls the destructor of each array element, there is currently no other difference (does not mean there won't be).
Since all versions of new/delete use malloc/free inside, there is no reason why delete should cause memory leaks.