C++ Vector Access Violation Run-Time Crash
I'm currently creating a stealth/detection system that uses a vector (the std C++ list, not points in 3D space) belonging the the NPC's AI to keep track of how close an NPC is to detecting another pawn. ~ Every .25 seconds, a "detect" function is called to tick all relevant entries in the DetectionVector. I've been testing it with one NPC detecting only the player, and it runs flawlessly until approx. 1 min of run-time where I get the crash report:
The relevant part of the function is here:
And the FindDetectionInfo function:
I have already debugged this, ensuring that only one entry in the vector is created, that it updates properly, etc. I have also determined that these lines are what cause the crash. At this point, I believe that there may be a UE4 flaw rather than a scripting error, so any help/advise would be appreciated!
Other info: Running on UE 4.6 Using Windows 7 x64
Vector is declared as: std::vector DetectionVector;
All unreal object pointers can become NULL. Eventhough they look as 'naked' pointers, they are managed.
The line: " if (DetectionVector.at(i)->getTarget() == target) " should check for if DetectionVector.at(i) != NULL.
Furthermore, as suggested use TArray instead of std::vector. This might be the reason Unreal things the object is not longer referenced and therefore collected by the gc.Hence it becomes an invalid pointer in your std::vector.
answered Feb 03 '15 at 07:57 AM
You should use TArray instead of std::vector. The std:: container classes do not play well with the way UE handles memory allocation, copying, moving, etc. Even if the problem is somewhere else in the code, the first suggestion you will receive when working with UE4 will be to use its own container types, not the stuff from the STL.
I would also like to point out that since you're putting UObjects inside of a container, you will need to declare the member property which is the container as a UPROPERTY(), otherwise the GC system will not be able to find any references to the UObjects, and they will be garbage collected, result in invalid pointers. You cannot declare a std::vector as a UPROPERTY, so you will need to use TArray, as I mentioned earlier.
Is UDetectionInfo derived from UObject? If so you should be using
answered Feb 03 '15 at 11:05 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here