Okey, im a memory noob when it comes to C++, i dunt really understand pointers and shallow-deep copy shit, and its causing me a problem.
Pointer to an image object, is passed to a class which makes some changes and uses it every step/frame. Works fine when theres only one instance of the object, but when i try to create another, my game crashes! Is passing a pointer to multiple instances bad??
Name:
Anonymous2007-05-11 23:46 ID:zts986Uq
It is if one of them destroys the object and the other tries to use free()'d memory.
Name:
Anonymous2007-05-11 23:47 ID:dz+aWhlf
Look into Singleton objects.
Name:
Anonymous2007-05-11 23:48 ID:b95LStoo
I'm not going to try to explain pointers in this tiny box.
Shallow copy = Just copy the object, leaving it as is; use this if the object doesn't change the state of anything else it has pointers to
Deep copy = Copy the object and also deep copy anything else it references, and then change the object to point to the new copies; use this if it does change the state of something it has pointers to, so you don't have multiple objects treating said things as though they have exclusive access to them.
As to your problem, if you mean that you're passing a single instance of an image object to several other objects, that might be a problem. If object A changes the image object to state foo and doesn't expect it to change while it's not looking, and then object B changes it to state bar, the next time object A does something with it and finds it in an unexpected state, exploding programs may result.
Name:
Anonymous2007-05-11 23:53 ID:jlF1C1ns
Think about it. Same pointer passed to, say, two instances of some class. Both of those classes now point at the same space in memory. Any changes in one will be seen by the other.
In particular, if you free the memory in one instance (using free() or delete or whatever), the other instance(s) will attempt to free it again. That'll crash.
>>2
WOO YOU FIXED IT! :D:D:D
It wasnt acctually the image object, i had an array of gridNode's that i was accidentally freeing when the enemy object was deleted, then i converted the enemy class to all use the same one, i forgot to remove the 'delete' line, so i removed that, and it woiks!! woo!
This is the first acctual help that ive ever gotten from 4chan.
Name:
Anonymous2007-05-11 23:59 ID:zts986Uq
Shit. Next time, I'm just going to sage you.
Name:
Anonymous2007-05-12 0:49 ID:qDmAthph
>>1
Shallow Copy: If there is no copy constructor, compilers generate code that only copies the actual bits of an object to the new object. This is the default action if you are not wise enough to create your own copy constructor.
Deep Copy: You have a copy constructor and it replicates members of dynamic built-in types and/or calls copy constructors of members of user defined classes and references the copies.
On a side note: if you don't understand pointers, you should stick to flipping... err, Java. 'Nuff said.
Name:
Anonymous2007-05-12 17:27 ID:7nydRjyl
>>8
Yeah i had a copy constructor, and java isnt available for PSP yet ;)