# TArray with custom class EXCEPTION\_ACCESS\_VIOLATION

**URL:** <https://forums.unrealengine.com/t/tarray-with-custom-class-exception-access-violation/482022>\
**Category:** C++\
**Tags:** error, tarray, unreal-engine, CPP, question\
**Created:** [April 16, 2021, 8:21pm UTC](https://forums.unrealengine.com/t/tarray-with-custom-class-exception-access-violation/482022 "2021-04-16T20:21:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mestereod](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mestereod](https://forums.unrealengine.com/u/Mestereod)\
**Post date:** [April 16, 2021, 8:21pm UTC](https://forums.unrealengine.com/t/tarray-with-custom-class-exception-access-violation/482022/1 "2021-04-16T20:21:30Z")

</div>

Unreal show me line “TilePtr& tile = m\_tiles[getTileIndex(pos)];” in TileBlock::getOrCreate()

TileBlock.h

```
#pragma once
#include "Tile.h"

class RPGONLINE_API TileBlock
{
public:
    TileBlock();

    const TilePtr& create(const Position& pos);
    const TilePtr& getOrCreate(const Position& pos);
    const TilePtr& get(const Position& pos) { return m_tiles[getTileIndex(pos)]; }
    void remove(const Position& pos) { m_tiles[getTileIndex(pos)] = nullptr; }

    uint getTileIndex(const Position& pos) { return ((pos.y % 32) * 32) + (pos.x % 32); }

    const TArray<TilePtr>& getTiles() const { return m_tiles; }

private:
    TArray<TilePtr> m_tiles;
};

```

TileBlock.cpp

```
#include "TileBlock.h"

TileBlock::TileBlock()
{
	g_logger.debug("TILEBLOCK CONSTRUCTOR");
	m_tiles.Empty();
	//m_tiles.SetNum(1024);
	m_tiles.Init(nullptr, 1024);
}

const TilePtr& TileBlock::create(const Position& pos)
{
	TilePtr& tile = m_tiles[getTileIndex(pos)];
	tile = TilePtr(new Tile(pos));
	g_logger.debug(FString::Printf(TEXT("Tile Created Index: %d, pos %d %d %d"), getTileIndex(pos), pos.x, pos.y, pos.z));
	return tile;
}

const TilePtr& TileBlock::getOrCreate(const Position& pos)
{
	TilePtr& tile = m_tiles[getTileIndex(pos)]; //LINE WITH ERROR
	if (!tile)
		tile = TilePtr(new Tile(pos));
	return tile;
}

```

Error appears when I call m\_tiles  
What I am doing wrong here?

---

<div class="post-metadata">

**Author:** ![staticvoidlol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@staticvoidlol](https://forums.unrealengine.com/u/staticvoidlol)\
**Post date:** [April 22, 2021, 9:16pm UTC](https://forums.unrealengine.com/t/tarray-with-custom-class-exception-access-violation/482022/2 "2021-04-22T21:16:10Z")

</div>

You need to decorate your TArray with UPROPERTY otherwise it gets garbage-collected.
