Random crashes in editor showing own code

I’ve been experiencing a lot of crashes during edition in the editor.
Using blueprints, changing tabs, saving, compiling different stuff.
And I noticed that in the reports most of them mention my own code: APLPlayerManager::AddPlayer()
Which is strange because all of this hapen during edition, not when playing the game.
This is what that funcion in particular has:

void APLPlayerManager::AddPlayer ( APLPlayer * _newPlayer )
{
	if ( _players.Contains( _newPlayer ) )
		return;

	_players.Add( _newPlayer );
}

_players is in APLPlayerManager.h declared this way:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Players)
TArray <APLPlayer*> _players;

Any thoughts on this? It’s pretty irritating I got say, it crashes like five times in just one hour.

Here’re some of the reports (1):

Access violation - code c0000005 (first/second  not available)

UE4Editor_ProyectoLoco!APLPlayerManager::AddPlayer() + 33 bytes [g:\dropbox\projects\proyectoloco\proyectoloco\source\proyectoloco\entities\player\plplayermanager.cpp:17]
UE4Editor_ProyectoLoco!APLPlayer::APLPlayer() + 122 bytes [g:\dropbox\projects\proyectoloco\proyectoloco\source\proyectoloco\entities\player\plplayer.cpp:11]
UE4Editor_CoreUObject + 1527933 bytes
UE4Editor_Engine + 4734865 bytes
UE4Editor_Engine + 5086842 bytes
UE4Editor_Engine + 3881259 bytes
UE4Editor_Engine + 3864915 bytes
UE4Editor_Engine + 3769424 bytes
UE4Editor_Engine + 3873565 bytes
UE4Editor_Engine + 3883827 bytes
UE4Editor_Engine + 3885994 bytes
UE4Editor_UnrealEd + 4144983 bytes
UE4Editor_UnrealEd + 4429648 bytes
UE4Editor_UnrealEd + 4526262 bytes
UE4Editor_UnrealEd + 1893329 bytes
UE4Editor_UnrealEd + 6486374 bytes
UE4Editor!FEngineLoop::Tick() + 3524 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\launchengineloop.cpp:2129]
UE4Editor!GuardedMain() + 479 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\launch.cpp:133]
UE4Editor!GuardedMainWrapper() + 26 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\windows\launchwindows.cpp:125]
UE4Editor!WinMain() + 249 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\windows\launchwindows.cpp:201]
UE4Editor!__tmainCRTStartup() + 329 bytes [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c:618]

Another (2):

Access violation - code c0000005 (first/second  not available)

UE4Editor_ProyectoLoco!APLPlayerManager::AddPlayer() + 33 bytes [g:\dropbox\projects\proyectoloco\proyectoloco\source\proyectoloco\entities\player\plplayermanager.cpp:17]
UE4Editor_ProyectoLoco!APLPlayer::APLPlayer() + 156 bytes [g:\dropbox\projects\proyectoloco\proyectoloco\source\proyectoloco\entities\player\plplayer.cpp:11]
UE4Editor_CoreUObject + 1527933 bytes
UE4Editor_Engine + 4734865 bytes
UE4Editor_Engine + 5086842 bytes
UE4Editor_Kismet + 3943925 bytes
UE4Editor_Kismet + 3612064 bytes
UE4Editor_Kismet + 3927904 bytes
UE4Editor_SlateCore + 753330 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_SlateCore + 753474 bytes
UE4Editor_Slate + 934138 bytes
UE4Editor_Slate + 932573 bytes
UE4Editor!FEngineLoop::Tick() + 3657 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\launchengineloop.cpp:2154]
UE4Editor!GuardedMain() + 479 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\launch.cpp:133]
UE4Editor!GuardedMainWrapper() + 26 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\windows\launchwindows.cpp:125]
UE4Editor!WinMain() + 249 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\windows\launchwindows.cpp:201]
UE4Editor!__tmainCRTStartup() + 329 bytes [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c:618]

Another (3):

Access violation - code c0000005 (first/second  not available)

UE4Editor_ProyectoLoco!APLPlayerManager::AddPlayer() + 17 bytes [g:\dropbox\projects\proyectoloco\proyectoloco\source\proyectoloco\entities\player\plplayermanager.cpp:17]
UE4Editor_ProyectoLoco!APLPlayer::APLPlayer() + 122 bytes [g:\dropbox\projects\proyectoloco\proyectoloco\source\proyectoloco\entities\player\plplayer.cpp:11]
UE4Editor_CoreUObject + 1527933 bytes
UE4Editor_Engine + 4734865 bytes
UE4Editor_Engine + 5086842 bytes
UE4Editor_Engine + 3881259 bytes
UE4Editor_Engine + 3864915 bytes
UE4Editor_Engine + 3769424 bytes
UE4Editor_Engine + 3873565 bytes
UE4Editor_Engine + 3883827 bytes
UE4Editor_Engine + 3885994 bytes
UE4Editor_UnrealEd + 4144983 bytes
UE4Editor_UnrealEd + 4429648 bytes
UE4Editor_UnrealEd + 4526262 bytes
UE4Editor_UnrealEd + 1893329 bytes
UE4Editor_UnrealEd + 6486374 bytes
UE4Editor!FEngineLoop::Tick() + 3524 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\launchengineloop.cpp:2129]
UE4Editor!GuardedMain() + 479 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\launch.cpp:133]
UE4Editor!GuardedMainWrapper() + 26 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\windows\launchwindows.cpp:125]
UE4Editor!WinMain() + 249 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.5\engine\source\runtime\launch\private\windows\launchwindows.cpp:201]
UE4Editor!__tmainCRTStartup() + 329 bytes [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c:618]

My specs are:
Windows 8.1,
i5-2400,
12gb Ram,
AMD 6870

What is this APLPlayerManager? Is it derived from actor? Is this some kind of component of another blueprint class? where you initialized it and how?

Hi,

As the class name said, APLPlayerManager is a manager for the players.

It’s a short class in C++, it’s called from other classes from blueprints to requests the players (right now there’s only one actually).

Here is the full class:

APLPlayerManager.h

#pragma once

#include "GameFramework/Actor.h"
#include "PLPlayerManager.generated.h"

#ifndef PL_PLAYER_H
#define PL_PLAYER_H
class APLPlayer;
#endif

UCLASS( Meta=(ChildCanTick) )
class PROYECTOLOCO_API APLPlayerManager : public AActor
{
	GENERATED_UCLASS_BODY()

	// Static

	static APLPlayerManager * Get() { return _instance; }

	// Dynamic

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Players)
	TArray <APLPlayer*> _players;

	void AddPlayer ( APLPlayer * _newPlayer );

private:
	
	static APLPlayerManager * _instance;
};

PLPlayerManager.cpp

#include "ProyectoLoco.h"
#include "PLPlayerManager.h"

APLPlayerManager * APLPlayerManager::_instance = NULL;

APLPlayerManager::APLPlayerManager(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	if ( APLPlayerManager::_instance == NULL )
		APLPlayerManager::_instance = this;

	_players.Empty();
}

void APLPlayerManager::AddPlayer ( APLPlayer * _newPlayer )
{
	if ( _players.Contains( _newPlayer ) )
		return;

	_players.Add( _newPlayer );
}

The player calls this function named AddPlayer to add himself to the list.

Are you spawning APLPlayerManager somewhere using GetWorld()->SpawnActor function?

Or maybe your actor is placed somewhere in the level?

Check if APLPlayerManager::_instance->GetName() contains “Default” keyword?

Actually not, I add the manager to the scene directly from start, I don’t spawn it in real time. The player is the one which is spawned from the gamemode.

Remember that the weird thing on this is that this is hapening during edition. Not when the game is playing.

This is extremaly important to check if your object is default one or a spawned instance. You should not operate on members of default object, I think you are doing it.

Sorry, I don’t get that. I have a member in the scene which assigns itself as the a static instance so it can be acceded from different places without needing a direct reference. Ofcouse when I call that static instance from different location, I check before if it’s NULL and also use the IsValid function.
Is it that I cannot use static variables in UE4?

You can use static variables but you have to make a reference to APLPlayerManager somewhere otherwise the GC will clean it.

You are telling that your code is crashing during editing only, so You can add

if (FApp::IsGame())
{

}

around AddPlayer function, this may help you.

Ok, I will try some things out and I would let you know.
What I don’t get is about “needing to reference the manager somewhere”. You mean in the objects from the scene? Does everything need to be referenced somewhere there?

Garbage Collection

Unreal implements a garbage collection
scheme where UObjects that are no
longer referenced or have been
explicitly flagged for destruction
will be periodically cleaned up. The
engine builds a reference graph to
determine which Objects are still in
use and which ones are orphaned. At
the root of this graph is a set of
Objects designated as the “root set”.
Any Object can be added to the root
set. When garbage collection occurs,
the engine can track all referenced
Objects by searching the tree of known
UObject references starting from the
root set. Any unreferenced Objects
will be assumed to be no longer
needed, and will be removed.

One practical implication here is that
you typically need to maintain a
UPROPERTY reference to an Object you
wish to keep alive. Actors are
frequently an exception to this, since
they are usually referenced by an
Object that links back to the root
set, such as the level to which they
belong. Actors can be explicitly
marked for destruction with Destroy().

Ok, got it but it doesn’t say anything about static instances though.
Anyway, I added the UFUNCTION to the AddPlayer and didn’t have a crash by the moment, I will continue testing it anyway.
Also tryed adding the UPROPERTY to the static instance but it doesn’t allow me.

Hi ,

As has mentioned, it is possible that the automatic garbage collection performed periodically by the Engine may be clearing out this class, then another class tries to access it. I would like to try to reproduce the crash you are seeing so I can try to determine exactly what is happening and provide a solution for you.

I created a class mirroring the one you posted above, and a Player class for the _players array. Could you provide a little more information about how and when you are calling Get and AddPlayer?

Also, if you see the crash happen again, can you get the log file for your project at that point and upload it here?

Hey ,

So right now, after removing the static functions and variables I managed to get it working without the mentioned crashes. I didn’t put it as resolved yet because I want to try it a little more during the day.

This maybe also related to another report I did, where I uploaded some specific code lines to test the crash and I added some file reports. Here:

Thank you

So, after removing the static methods and variables I got this resolved. It’s maybe not the solution I wanted but it’s okey I can work without the statics in this case.
Thanks and for the support.

Hi ,

I did a little more investigation into this issue this morning, and was able to get some information that may be helpful to you, as well as future users who may run into this issue.

Unfortunately using static methods and variables within Unreal Engine 4 is not a particularly safe practice. It is not impossible, but statics are essentially invisible to the Engine’s garbage collection system, and can lead to completely unexpected behavior (crashes, stale values, inability to unload levels, etc).

A suggestion that would possibly work better within the Engine would be to place the variables in WorldInfo, GameMode, or GameInstance, depending on your needs.

PS. I will copy this comment over to your other post as well.

Ok, got it, .

I suggest you guys mention this in the decumentation. Because it has been pretty frustrating with the crashes until I get this working. And there’s probably more people who prefer using static stuff.

Best,

Tobias

I just wanted to provide a quick update for you. I have entered a request to have some additional information about statics included in the documentation (UEDOC-737). Our docs team is usually fairly busy, but this will put the request in front of them to be considered for inclusion in the documentation in the future.

Ok, thank you, .
Cheers!