EXCEPTION_ACCESS_VIOLATION when trying to return TArray<>

Having a strange Error. I have a UE module which uses a dynamic library. Inside the library I create on a stack a std::vector<std::pair<struct, int>>> and return the created object by copy.

Inside UE module I loop through items and add them to a TArray<FTransform>. Then return by copy(?). And suddenly I have an Error which is really strange.

EXCEPTION_ACCESS_VIOLATION writing address 0x0000000000000010

UE4Editor_Core!__TBB_LockByte()
UE4Editor_Core!rml::internal::Block::freePublicObject()
UE4Editor_Core!scalable_free()
UE4Editor_Core!FMemory::Free() [C:\UE_4.26.1_Source\Engine\Source\Runtime\Core\Public\HAL\FMemory.inl:80]

TArray<FTransform> UGMazeGeneratorActorComponent::GenerateMaze() const
{
    TArray<FTransform> Grid;

    for (const auto& item : graph_r.getRealWorldCoords()) //copy of std::vector<std::pair<struct, int>>> returned by value
	{
		const Vertex from = item.first;
		const float rotation = item.second;
		const float X = from.column * m_WallHeight;
		const float Y = from.row * m_WallWidth;

		const auto wall = FTransform(FRotator(0, rotation, 0), FVector(X, Y, 0));

		Grid.Add(wall);  
	}  // this line EXCEPTION_ACCESS_VIOLATION writing address 0x0000000000000010

	return Grid;
}

std::vector<std::pair<Vertex, int>> Graph::getRealWorldCoords() const
{
    std::vector<std::pair<Vertex, int>> realWorldCoords;
    realWorldCoords.push_back(std::make_pair(Vertex(x1, y1), 0));

    return realWorldCoords;
}

If I use my custom iterator to loop through std::vector<std::pair<struct, int>>> which returns an item by the reference everything is ok.

You really should use TArrays, TSets, and TMaps instead of the standard containers. I think it has to do with UE’s garbage collection and reflection systems.

Nacho, OP is using the standard containers inside an external library.

Unfortunately, i’m not much help here, other than “hit it with the debugger and see what’s happening”

Nacho is right, the problem with GC. It is in general a bad idea to return a STL container from a dll. So I decided to rewrite my dll using good practices of developing APIs.