Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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:

 * TRUE, if the team was added to the team manager.
 * @param teamType    The team to create and add
 * @param team        the created and added team or NULL
 * @param bUnique    should only be one team of this type (default = true)
 * @return true: team added by team manager, false: else
 bool UTeamManager::CreateAndAddTeam(TSubclassOf<class UBaseTeam> teamType, UBaseTeam* team, bool bUnique)
     bool        ok        = true;
     if (bUnique || bForceUniqueTeamTypesOnly)
         ok = !HasTeamType(teamType);
     // Creates and adds the team
     ok = ok && CreateTeam(teamType, team);
     ok = ok && (team != NULL);
     ok = ok && AddTeam(team);
     if (!ok)
         team = NULL;
     return ok;

 * Creates the team from a team type.
 * @param teamType    The type of the team
 * @param team        The team created (or null)
 * @return            true: successful, false: else
 bool UTeamManager::CreateTeam(TSubclassOf<class UBaseTeam> teamType, UBaseTeam* team)
     bool okay = false;
          team = NULL;
     team = ConstructObject<UBaseTeam>(
     okay = (team != NULL);
     return okay;

When looking at the runtime variables, the following can be observed:

  • After calling ConstructObject in the function CreateTeam-function (just before returning to the calling function) After ConstructObject Call

  • Directly after returning from CreateTeam in the CreateAndAddTeam-function After Returning from the CreateTeam Function

Thanks for your help.


Product Version: Not Selected
more ▼

asked Nov 22 '14 at 10:39 AM in C++ Programming

avatar image

33 5 7 10

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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:

 bool UTeamManager::CreateAndAddTeam(TSubclassOf<class UBaseTeam> teamType, UBaseTeam** team, bool bUnique)
     // ... snip ...

     ok = ok && CreateTeam(teamType, team);
     ok = ok && (*team != nullptr);
     ok = ok && AddTeam(*team);

     if (!ok)
         *team = nullptr;

     return ok;

 bool UTeamManager::CreateTeam(TSubclassOf<class UBaseTeam> teamType, UBaseTeam** team)
     //... snip ...

    *team = ConstructObject<UBaseTeam>(/* snip */);
    okay = (*team != nullptr);
    return okay;

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.

more ▼

answered Nov 22 '14 at 04:39 PM

avatar image

584 30 14 38

avatar image Danneran Nov 22 '14 at 05:28 PM

Ah, yes. My experience with C++ is a bit rusty, but you are totally correct, I need to do this pointer-pointer thing (or alternatively rewrite the function). Sorry for this stupid question and thank you very much for your help!

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question