HEAP CORRUPION DETECTED: afer Normal block (...)
CRT detected that the application wrote to memory after end of heap buffer.
this error occurs when i try to delete certain instance. I am pretty sure that im not double deleting things, use proper delete. Heres fragment of code:
Heap corruption occurs when you dereference a bad pointer. This results in memory being corrupted. However you don't find out about it until later (when memory is cleaned up).
Basically what I'm getting at is that even though you get the error on a delete, that's not where the problem is happening. That's just when it's being discovered. The actual problem is elsewhere.
Off the top of my head, I spot a potential problem:
- You have multiple ctors but you only posted the code for one of them. Are you new'ing those objects in your other ctors as well?
- You don't have any copy constructor or assignment operator. You'll need to write those of you will shallow copy your pointers which is problematic
But really, the big issue is....
- Why are you using new for this in the first place? It just makes your code more complicated and error prone. I would get rid of it altogether and just make qMag, qWait, empl, etc all objects instead of pointers. Then you don't need to worry about new/delete at all.
I do not have copy ctor or assignment operator cause i do not need one atm.
Im using pointers cause earlier version of this project was using them. I'll try to change them to objects but still i want to know what might be causing this error.
cAffiliate(void);
cAffiliate(int id, int emp);
cAffiliate(int id);
But you only showed the code for one of them. Are the other two also using new to allocate those objects? If not, they need to be.
I do not have copy ctor or assignment operator cause i do not need one atm.
It's possible you are accidentally copying objects, which would blow a hole in everything. With these kinds of classes where memory is managed you should always provide a copy constructor and assignment operator. If you really don't need them, then make them private and don't give them a body:
1 2 3 4 5 6 7 8
class cAffiliate
{
//...
private:
cAffiliate(const cAffiliate&); // don't give them bodies
cAffiliate& operator = (const cAffiliate&);
};
This way, if you are accidentally copying the objects somewhere, you'll get a nice compiler error instead of having your program explode.
I'll try to change them to objects but still i want to know what might be causing this error.
It's a bad pointer dereference. It could be anywhere in the program, unfortunately. It's not necessarily in the code you posted. Heap corruption can be really hard to track down.
cAffiliate(void);
cAffiliate(int id, int emp);
cAffiliate(int id);
But you only showed the code for one of them. Are the other two also using new to allocate those objects? If not, they need to be.
They are doing the same thing with qWait, qMag and empl and besides, I'm only using only ctor posted above.
Added copy ctor and assigment operator as you advised me but still i got the same error.
I have narrowed program only to initialization and cleaning up - error still shows up. In debug mode everything was looking fine...
About initialization: functon is reading a text file and creates propper structures - uses only PushFirst and PushBack functions posted below.