Calling ConstructObject in a Function Returning NULL
I'm trying to figure out why an object created with ConstructObject is set to NULL when leaving the function where it was created. I assume that this is somehow related to the GC, but I do not understand why and what I can do against it.
My CreateAndAddTeam-function will call CreateTeam, where I construct a new team. In CreateTeam, the team is properly constructed, meaning, not NULL. But after returning to the calling function (in this case CreateAndAddTeam), the returned pointer is set to NULL.
Below is the code:
When looking at the runtime variables, the following can be observed:
Thanks for your help.
asked Nov 22 '14 at 10:39 AM in C++ Programming
It's not the GC, you're changing what the team pointer in CreateTeam() points to but this is not the same pointer as the team in CreateAndAddTeam(), it's just a copy. So the original team pointer in CreateAndAddTeam() remains unchanged (and thus null if it was null when you called CreateTeam()). If you want to change the original pointer you have to pass a pointer to it (a pointer to a pointer), like so:
Of course since neither of those methods actually make use of the original value of the team pointer it doesn't really need to be a parameter and you could just return a pointer to the new team object (or nullptr in case of failure) instead of returning a boolean.
answered Nov 22 '14 at 04:39 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here