Well... this issue is so weird i don't know how to synthesize it...
I'm just gonna post the code here, and tell you what i've figured out so far.
Feel free to test it and tell me what output you get ! (Only Allegro lib and std used)
This is going to be some sort of space game, yet its more a test for me to learn than an actual game
main.cpp
Chunk.h
Chunk.cpp
AstronomicalObject.h
AstronomicalObject.cpp
Planet.h
Planet.cpp
Outcome:
i get a plain green circle on my display, as expected, then i get 5 screens with some green and grey hollow circles... the thing is i should get grey hollow circles and PLAIN green circles... Something is going wrong.
There we are, these are all the sources i've got.
What it seems is the virtual draw function from AstronomicalObject overriden by Planet does not do what its supposed to do (no runtime link resolution).
No matter whether the pointer points to a Planet or an AstronomicalObject, it will call the AstroObj version of the draw function.
I've spent more than 5 hours trying to debug and understand what goes wrong, but... it just makes no sense ! It i supposed to work, that's what virtual functions are made for !
I really hope you will discover where the issue come from, cuz i'm dispairing !
Thx for you help ! <3
PS: As you may have noticed, i'm quite new to programming, so maybe the issue is dumb easily fixable, and i'm just too bad to find out how to.
If you want people to try your code, provide a zip file with the sources in it.
Of course !
EDIT: Sorry the link didnt work ! You have it attached on my next post.
There you go ! (Im working with code::blocks, and i'm using MinGW64 Compiler)
Your link doesn't work.
{"name":"611634","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/6\/a626c7009a4e4585ac18f6a2a43739da.png","w":1100,"h":552,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/6\/a626c7009a4e4585ac18f6a2a43739da"}
Just attach the zip file of your source.
Nani ??
Well, there you go then :p
Every time you assign testChunk to a new Chunk(true) object on the stack you reference destroyed memory.
testChunk = Chunk(true);
This calls Chunk::operator=(const Chunk& c);. The default assignment operator merely copies the data, so your vector<AstronomicalObject*> gets copied. This is a shallow copy. Now you have two copies of the same astronomical objects. Then the temporary Chuck(true) object goes out of scope, deleting every single object that was in it. Now your testChunk object holds pointers to deleted memory. That is why it segfaults.
2. You're using 'const' wrong. It goes at the end of the function declaration before the semi-colon.
class X { public : void function() const;/// I promise I won't change anything };
Oh
My
God
I still dont understand why it didnt work (i will definitely think about what you said, and try to understand) but using a pointer to a Chunk object instead of the Chunk object itself solved the sh*t !
My main.cpp is now
and the virtual function does its job !!
Now your testChunk object holds pointers to deleted memory. That is why it segfaults.
The problem is not a crash (it doesnt crash, no segfault here), the problem was the wrong version of the draw() function was called.
your vector<AstronomicalObject*> gets copied. This is a shallow copy. Now you have two copies of the same astronomical objects.
The AstronomicalObjects dont get copied, since vector<AstronomicalObject*> holds pointers, not actual objects. So when it copies, it copies the adress stored in the pointers too. The actual AstronomicalObject is still being referenced to by these copied pointers : no memory leaks here. Where am i wrong ?
When you do testChunk = Chunk(true) doesnt it implicitly calls delete on the previous content of testChunk ?
I mean if you have something like std::string myString = "Thank You Very Much For Your Help";
then you do myString = "REALLY"; it doesnt leak memory right ?
You're using 'const' wrong. It goes at the end of the function declaration before the semi-colon.
My bad ! Thx !
Thank you so much anyway ! You saved my day once again !
Now your testChunk object holds pointers to deleted memory. That is why it segfaults.
The problem is not a crash (it doesnt crash, no segfault here), the problem was the wrong version of the draw() function was called.
I ran it through gdb, and it segfault'ed. Same code. That's what you get when you dereference bad memory. You get undefined behavior.
your vector<AstronomicalObject*> gets copied. This is a shallow copy. Now you have two copies of the same astronomical objects.
The AstronomicalObjects dont get copied, since vector<AstronomicalObject*> holds pointers, not actual objects. So when it copies, it copies the adress stored in the pointers too. The actual AstronomicalObject is still being referenced to by these copied pointers : no memory leaks here. Where am i wrong ?
Yes, the pointers get copied. But when your temporary object goes out of scope, those pointers get deleted. This means any copies of those pointers now hold references to deleted memory, which will make it segfault.
but using a pointer to a Chunk object instead of the Chunk object itself solved the sh*t !
That's because you stopped copying your vector. You need to learn about scope and the stack and the heap. Learn about the difference between a shallow copy and a deep copy.
Look at this code :
If you run it you can see the output and that it crashes because it destroys the same memory twice. That is what you were doing when you copied your vector using a shallow copy.
GoodOwner : Deleting base 00ca1620 GoodOwner : Deleting base 00ca1600 BadOwner : Deleting base 00ca2f30 BadOwner : Deleting base 00ca2f30
Thank you very much ! I'll work on it.
Edit:
Yes, the pointers get copied. But when your temporary object goes out of scope, those pointers get deleted. This means any copies of those pointers now hold references to deleted memory, which will make it segfault.
Oh, so what you mean is when this temporary object goes out of scope it implicitly calls the destructor function of every object pointed by the pointers held in this temporary object ? (i'm trying to reformulate to be sure i have well understood what you said ^^) Is that correct ? (i really must look again at basic class definition and operator method overriding courts xD)
Edit Edit:
I'm stupid...
It just calls the destructor of the Chunk class, which i told to delete everything.........
Sorry, i've been kinda slow on this one xD
Oh, so what you mean is when this temporary object goes out of scope it implicitly calls the destructor function of every object pointed by the pointers held in this temporary object ? (i'm trying to reformulate to be sure i have well understood what you said ^^) Is that correct ? (i really must look again at basic class definition and operator method overriding courts xD)
To answer the specific point in bold, no. And yes, because you told it to. When a pointer goes out of scope, nothing happens. When an object goes out of scope, it's destructor is called. Which in this case, was Chunk::~Chunk, which deleted every pointer in the object vector.