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"

Violation Error on Add to Tarray


I am trying to implement an inventory system into my game. I have made an items class which inherits from AActor, a Character class and an inventory class.

The Item is passed as a pointer to a function on Inventory->AddItemToInventory(Item* ItemToInsert); using the Hit.GetActor() method with a static_cast to the item.

All the AddItemToInventory does is call the Add method on the TArray ItemsArray (defined in the header file of Inventory. There is no constructor on the inventory and is created in the characters constructor Inventory* PlayerInventory = new Inventory();


When I attempt to perform this action I get an Access Violation error:

 Exception was "Access violation - code c0000005 (first/second chance not available)"
Product Version: UE 4.10
more ▼

asked Jan 18 '16 at 05:43 AM in C++ Programming

avatar image

32 7 7 8

avatar image cancel Jan 18 '16 at 06:03 AM

There are many things that could cause this. Could you post the .h and .cpp files that contain the Inventory and also the thing that uses the Inventory?

avatar image hydroflax Jan 18 '16 at 06:30 AM

sure. (the comment system is being a little bit weird for me atm, not doing anything when I press comment for a long comment so have attached it as a text file. Note the UsableItem constructor just has a simple static mesh.

Inventory Code

code.txt (8.5 kB)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

The reason your code is crashing is because your Inventory class and instances are not being tracked by the UObject system in UE4. This means that the pointers to your objects in the TArray ItemsArray can become invalid.

You should change your inventory class declaration to be a UStruct instead, and look like this:

 struct GRAVITYGAME_API FInventory
         void AddItemToInventory(AUsableItem* ItemToInsert);
         void RemoveItemFromInvetory(AUsableItem* ItemToRemove);
         // An array which stores the picked up items.
         UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Array)
         TArray<AUsableItem*> ItemsArray;

(sorry if the formatting was messed up by Answerhub)

You don't need a constructor. There's nothing for it to do.

This: Inventory* PlayerInventory;

needs to become this: FInventory PlayerInventory;

You should almost never use the 'new' keyword in UE4, except in certain cases such as dealing with the lower-level parts of Slate. There's no reason for you to create an Inventory object as a raw pointer and create it by hand. It's not accomplishing anything, it makes things more complicated, and it prevents the UE4 object system from working with it.

By changing it to be a struct (specifically, a UStruct) instead of a pointer to a raw C++ class, it will correctly work as a UPROPERTY, the pointers to UObjects within the inventory array will be tracked correctly by UE4, and you will not need to do manual memory management.

As an example of why you shouldn't have been doing the manual memory management by hand: your game was leaking every Inventory object you created, because you did not destruct it properly. Also, it would have not worked correctly when creating new instances of your AMainCharacter class from Blueprint subclasses in the editor, since it didn't deal with copying/CDO correctly.

Also, the UPROPERTY macro you were using in your Inventory class was doing nothing -- it only does stuff if you have a GENERATED_BODY() and the class or struct is a UCLASS or a USTRUCT.

Hope that fixes it for you!

more ▼

answered Jan 18 '16 at 07:56 AM

avatar image

2.4k 101 72 124

avatar image hydroflax Jan 19 '16 at 07:04 AM

Thanks man. Worked like a charm, I'm not sure what I was thinking when I decided I would make it a class cos an inventory doesn't really represent an object but tracks them instead.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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