x

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"

Crash when accessing UPROPERTY() member of USTRUCT()

I have a USTRUCT() declared like so in Weapon.h:

 USTRUCT()
 struct FWeaponData
 {
     GENERATED_USTRUCT_BODY()
     
     ...
     
     UPROPERTY(EditDefaultsOnly, Category = Config)
     int32 Priority;
 };
 

As well as a reference to it in the class declaration:

 UCLASS()
 class WEAPONESSENTIALS_API AWeapon : public AActor
 {
     GENERATED_BODY()
 public:
     
     ...
     
     UFUNCTION(BlueprintCallable, Category = Accessors)
     FWeaponData GetWeaponConfig();
     
     ...
     
     protected:
     
     UPROPERTY(EditDefaultsOnly, Category = Config)
     FWeaponData WeaponConfig;
    
 ...
 };

GetWeaponData() is an accessor method with the following implementation:

 FWeaponData AWeapon::GetWeaponConfig()
 {
     return WeaponConfig;
 }

The data in WeaponConfig is supposed to be set in the defaults of any blueprint that inherits from AWeapon. In the code of AWeaponEssentialsCharacter (in WeaponEssentialsCharacter.cpp), there is some code that attempts to access a member of the USTRUCT() that results in an EX_BAD_ACCESS:

 void AWeaponEssentialsCharacter::ProcessWeaponPickup(AWeapon *Weapon)
 {
     if (Weapon != NULL)
     {
         if (CurrentWeapon == NULL) // No weapon is currently equipped
         {
             AWeapon *Spawner = GetWorld()->SpawnActor<AWeapon>(Weapon->GetClass()); // EXC_BAD_ACCESS here
             
             if (Spawner) // Valid weapon
             {
                 Spawner = CurrentWeapon;
                 
                 int32 SwitchValue = Spawner->GetWeaponConfig().Priority;
                 
                 ...
                 
             }
         }
     }
 }
 

Unless I wasn't paying proper attention, I did not see anything in the tutorial that initializes FWeaponData WeaponConfig in the C++ code. However, either the member or the USTRUCT() is null, or the USTRUCT() itself is null. What seems to be the issue in the handling of the USTRUCT() in question?

All of the above code is from a project adapted from the following YouTube tutorial series:

https://www.youtube.com/watch?v=gJJCkLrK3gs

Product Version: Not Selected
Tags:
more ▼

asked Apr 16 '15 at 12:48 AM in C++ Programming

avatar image

Alonzo Machiraju
62 12 15 20

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

1 answer: sort voted first

Any "Access" crashes means you trying to do something on non-existant (NULL) or not valid thing

      if (CurrentWeapon == NULL)  //By that CurrentWeapon will be always NULL inside this scope 
      {
          AWeapon *Spawner = GetWorld()->SpawnActor<AWeapon>(Weapon->GetClass()); //You set Spawner
          
          if (Spawner) // You check if spawn succed and Spawner is set, not NULL
          {
              Spawner = CurrentWeapon; //For some odd reason you set Spawner to CurrentWeapon... which always be NULL
              //So now Spawner is also NULL 
              int32 SwitchValue = Spawner->GetWeaponConfig().Priority; //So now you calling GetWeaponConfig() on NULL..... BOOM!
              
              ...
              
          }
      }
more ▼

answered Apr 16 '15 at 02:09 AM

avatar image

Shadowriver
35.2k 921 166 1098

avatar image Alonzo Machiraju Apr 17 '15 at 06:59 PM

It turns out that Spawner = CurrentWeapon should've been CurrentWeapon = Spawner. I made a typo and entered the assignment in the wrong order, then found myself unable to spot the error. Thanks for the 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