Advertisement

Problem with this code?

Started by August 07, 2000 11:26 PM
28 comments, last by Esap1 24 years, 4 months ago
esap1 I was just in the shower where I do my best thinking and now im almost SURE your problem is with a copy constructor. WHENEVER you write a class that allocates any kind of memory you have to write you own copy constructor that does a deep copy of the object. In essence here is what you are doing somehow.

Create new object- lets call him jimmi.

do SOMETHING that is invoking the default copy constructor-
lets call the copy Bob.

Bob does not allocate his own memory. Instead he has to use jimmi''s memory.

Bob goes out of scope and gets sent to the destructor which in turn delete''s jimmi''s memory.

jimmi goes out of scope
jimmi gets sent to teh destructor....OH NO! Jimmi has no memory to delete cuz that ASS Bob already gave it to the destructor.

BAD THINGS happen!

hope that helped,
skitzo_smurf
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
Thank you for actually spending time on my problem.
You said:"WHENEVER you write a class that allocates any kind of memory you have to write you own copy constructor that does a deep copy of the object"
I dont really understand. Could you please help me by showing me some code I might use for a copy constructor?
Thanks a lot for you time.
Advertisement
Now I dont think I really dont think I understand
esap1, a copy constructor is invoked when you do anything that needs a copy of an object. For example, passing by value -

void someFunc(Object obj)//this is a COPY of original
{ //and invokes the copy constructor
//do something with obj
} //obj goes out of scope- destructor called

void main()
{
Object original; //lets say original allocates some memory
// for itself

someFunc(original); //invokes copy constructor

/* at this point original''s memory has been deleted by the
copy in someFunc. */
}

You have two solutions. You can pass by reference which does not call the copy constructor.

void someFunc(Object &obj); //no copy made this time

or you can write your own copy constructor that does a deep copy versus a shallow copy(which is what happens in a default copy constructor).....err I dont have time to give you a tutorial on that right now. Basically though a deep copy allocates its own block of memory. Im sure someone here will give you an example of a deep copy constructor..I hafta go!!

later,
skitzo_smurf

"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
Esap1 - Try doing this in your debugger.

Once you hit the Access Violation and pop into debug mode, refer to the toolbox that is labeled "Debug". There is a button there that will show what is called the "Call Stack". This call stack will show you the progression of function calls that have led to the calling of the destructor. If you see a call to the destructor from a place where you know such a call should not come from, refer to that bit of code for it most likely will be the culprit. The debugger is very handy for finding mysterious bugs like the one that is now haunting you. I hope I have helped you.

Mike Maxim
mmaxim@maximindustries.com
s0k
Thanks All, I FINALLY FIXED IT. I am now in the proccess of changing every thing to pointers and new''ing them. It works much better. I am also checking a million precations before I new or delete anything. Thanks Again for your time, it was greatly Apreciated
Advertisement
If the error occurs in the first line of his destructor (like on "isOK = false;") the problem can''t be with a copy constructor not allocating memory. If it was, then the error would only occur where memory is being freed (a "delete" statement). But check it out anyway. Lord knows how many bugs I caught checking out thing that had (at first glace) not much to do with the problem .

What it also could be is that you pass your COctree variable as a pointer to another class or function that deletes it. Like this:

    class Blabla{  COctree *tree;public:  Blabla(COctree *p) { tree = p; }  ~Blabla() { delete tree; }};void main(){  COctree tree;  Blabla bla(&tree);}    


This code will also give an access violation, because the compiler attempts to destroy the "tree" variable, but the "bla" object has already destroyed it.

Hope this helps,
Dormeur
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
Damn, just one minute too late .
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
Hi there!

What I wonder:

Is the line

COctree *child[8];

really declaring 8 pointers to Octrees or isn''t it declaring
ONE pointer to and ARRAY of 8 Octrees.

Try this:

COctree (*child)[8];

(Just an idea...)

Regards, Thorsten
Esap1,
check your mail. I have sent you a tutorial on what you need to know.

hope I solve your problem,
skitzo_smurf
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf

This topic is closed to new replies.

Advertisement