1

I am getting a heap corruption error when trying to compile my program. The code in question is a pointer

cparticle * particles. 

It is initialized to NULL and then set to

particles = new cparticle[amount]

I am only using delete once in the destructor, and it is causing windows to trigger a breakpoint. I have attempted to use application verifier, and it give me this info:

===========================================================
VERIFIER STOP 0000000000000013: pid 0x17C0: first chance access violation for current     stack trace 

000000001D54A0A4 : Invalid address being accessed
0000000055741DC6 : Code performing invalid access
000000000025E9D0 : Exception record. Use .exr to display it.
000000000025E4E0 : Context record. Use .cxr to display it.
===========================================================
This verifier stop is continuable. 
After debugging it use `go' to continue.
===========================================================



=======================================
VERIFIER STOP 00000013: pid 0x17C0: First chance access violation for current stack trace. 

1D54A0A4 : Invalid address causing the exception.
55741DC6 : Code address executing the invalid access.
001DF30C : Exception record.
001DF35C : Context record.


=======================================
This verifier stop is continuable.
After debugging it use `go' to continue.

=======================================

I am unsure what I am doing wrong, so any help will be appreciated.

user975989
  • 2,578
  • 1
  • 20
  • 38
  • 1
    It'll be hard for us to debug this unless we see some more code. – Mysticial Oct 08 '11 at 17:11
  • 1
    Showing the code would help to figure out what you went wrong. – Vinzenz Oct 08 '11 at 17:13
  • 1
    Agree with @Mysticial and @Vinzenz. As a first question; did you use `delete` or `delete []`? – Ben van Gompel Oct 08 '11 at 17:15
  • A couple of clarifications are needed. First - does this happen when you run the program, or (as I think you mistakenly wrote) when you *compile* it? Second - post your constructor and destructor logic (if not the whole class). Often it helps to add a "cout" to the dtor, so you can tell if there is some unexpected extra delete occuring. – holtavolt Oct 08 '11 at 17:17
  • Sorry, it happens when I run the program. The destructor is just delete [] particles, and the constructor is just copying input and particles = NULL; particles = new cparticle[amount]; – user975989 Oct 08 '11 at 17:39
  • [possibly related](http://stackoverflow.com/questions/4172722/) – fredoverflow Oct 08 '11 at 17:41
  • We need to see some code to determine what is happening. – John Yang Oct 08 '11 at 19:13

2 Answers2

3

Have you reproduced this in a small standalone program? Are you sure it's not caused by some other memory violation from before that went undetected until now? Are you using the correct delete operator?

megazord
  • 3,210
  • 3
  • 23
  • 31
2

The first thing you're doing wrong is you're not using std::vector<particle>.

The second thing is presumably that you're writing to memory past the end of your array of particles.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
  • Oh, I thought vectors take up much more space and are heftier to use than arrays. Oh well, I;ll just switch it. – user975989 Oct 08 '11 at 17:37
  • Vectors take up slightly more space and you shouldn't worry about performance with them. That said, using arrays is perfectly fine - c++ allows both after all. – Pubby Oct 08 '11 at 17:42
  • 2
    This is a silly comment. You might prefer that he use `vector` but it is not the use of a raw pointer that makes his code incorrect. Therefore your setup ("The first thing you're doing wrong ...") just comes off as inflammatory to me. You may have very good reasons for suggesting STL, but this is not a good way to advocate for your preferred style and beside the point is somewhat irrelevant to the question. – asveikau Oct 08 '11 at 17:49
  • @asveikau - Sorry you were offended, but glad the OP apparently wasn't and took the advice as it was intended. He/she asked "what am I doing wrong?" Using a raw pointer to a buffer is wrong. Nothing inflammatory about it - just a bonus piece of advice that will be helpful in the long run. – Daniel Earwicker Oct 08 '11 at 17:52
  • Ok I changed it to vectors, and I found the root of the problem. In the for loops I accidentally accessed amount instead of the iterator, which fixed the problem. I ran into a problem with boost though, but I think I can fix that myself. Thanks for the help. – user975989 Oct 08 '11 at 17:54
  • 2
    No problem. This is just one great advantage to using high-level collection classes: it is harder to use them incorrectly. With pointers, an infinity of bad behaviours is as easy to access as the small set of desirable behaviours. – Daniel Earwicker Oct 08 '11 at 18:00
  • @DanielEarwicker So if it's done correctly it's also "wrong"? This is a curious definition of "wrong" to me... Again I have no qualms with the suggestion and I understand why you make it, it's just very silly that *correct* usage could be called "wrong". It's rather like saying 1 + 1 is not 2 because I didn't arrive at it your favorite way - if you have a preferred method for addition it's fine that you advocate it but I don't think it's necessarily right to trash another method. – asveikau Oct 08 '11 at 18:02
  • 1
    @asveikau - I refer you to the book you ought to read before commenting further on this subject: Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4 Chapter C.14.11 *Prefer vector over array*. As Stroustrup explains, there is really no good reason to use raw new-allocated arrays. Not even for efficiency: sometimes people mistakenly use an array so they will be able to transfer ownership of some data without copying it. But see my answer to this question: http://stackoverflow.com/questions/599308/proper-stack-and-heap-usage-in-c/599573#599573 – Daniel Earwicker Oct 08 '11 at 18:05
  • @DanielEarwicker As I say, I know the reasons, I know C++ and I know STL. I'm just saying that there is nothing *wrong* with *correct* C-style code. You can say you should avoid it in C++ and I understand this argument, that does not make the code incorrect. – asveikau Oct 08 '11 at 18:34
  • "There is nothing wrong with correct C-style code" - then why did you bother to learn C++? Just learn C, save yourself a lot of study time. There would be "nothing wrong" with your programs. – Daniel Earwicker Oct 08 '11 at 18:47
  • What I'm saying is there is a difference between idiom/style and correctness. The problem here is out of bound access, the style point is fair but secondary. You present it as the primary problem. – asveikau Oct 08 '11 at 19:00