|
-
May 29th, 2002, 11:44 AM
#1
New and Delete
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:
PHP Code:
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 error
Debug 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 [i]delete array; 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
-
May 29th, 2002, 11:47 AM
#2
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 29th, 2002, 12:37 PM
#3
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:
PHP Code:
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:
PHP Code:
int* i = new int;
delete i;
int* j = new int [3];
delete[] j;
Again, thanx.
Destined
-
May 29th, 2002, 12:40 PM
#4
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 29th, 2002, 01:18 PM
#5
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
-
May 29th, 2002, 01:35 PM
#6
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. It seems to work, now that I've fixed the bounds error.
Thanx, though, Parksie, for the info on the delete opertor. 
Destined
-
May 29th, 2002, 01:42 PM
#7
Monday Morning Lunatic
Originally posted by Destined Soul
You gotta love C++ and being able to go out of bounds of the arrays and still write data.
By-product of C.
How d'you think the Internet Worm worked? (as in the original one, many years ago) It overwrote past the end of a buffer and injected its own program code in.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 31st, 2002, 03:04 AM
#8
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|