# Editor crashes on memcpy

**URL:** <https://forums.unrealengine.com/t/editor-crashes-on-memcpy/451638>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [July 3, 2019, 1:24pm UTC](https://forums.unrealengine.com/t/editor-crashes-on-memcpy/451638 "2019-07-03T13:24:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aseptio](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Aseptio](https://forums.unrealengine.com/u/Aseptio)\
**Post date:** [July 3, 2019, 1:24pm UTC](https://forums.unrealengine.com/t/editor-crashes-on-memcpy/451638/1 "2019-07-03T13:24:35Z")

</div>

Hi ! I am currently working on a volume rendering project.

By using the data from a custom format, I must dynamically generate a Volume Texture and then a material using a custom shader.  
Here is the code I use to generate my texture:

```
void VolumeRenderingManager::createVolumeTexture(std::string name, int sx, int sy, int sz, unsigned char* data)
{
	_width = sx;
	_height = sy;
	_depth = sz;

	const unsigned long TotalSize = _width * _height * _depth * 4;

	// Creating package and resource
	std::string ManualName = "T_" + name + std::to_string(_id);

	FString PackageName = TEXT("/Game/GeneratedTextures/");
	FString TextureName = ManualName.c_str();

	PackageName += TextureName;
	UPackage* Package = CreatePackage(NULL, *PackageName);
	Package->FullyLoad();

	IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();

	FString RelativePath = FPaths::ProjectContentDir();

	_volumeTexture = NewObject<UVolumeTexture>((UObject*)Package, FName(*TextureName), RF_Public | RF_Standalone | RF_MarkAsRootSet);

	_volumeTexture->AddToRoot(); // No garbage collection

	_volumeTexture->PlatformData = new FTexturePlatformData();
	_volumeTexture->PlatformData->SizeX = _width;
	_volumeTexture->PlatformData->SizeY = _height;
	_volumeTexture->PlatformData->NumSlices = _depth;
	_volumeTexture->PlatformData->PixelFormat = EPixelFormat::PF_B8G8R8A8;

#if WITH_EDITORONLY_DATA
	_volumeTexture->MipGenSettings = TMGS_NoMipmaps;
#endif

	_volumeTexture->SRGB = false;
	_volumeTexture->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;

	_volumeTexture->UpdateResource();

	// Creating mip
	FTexture2DMipMap* Mip = new(_volumeTexture->PlatformData->Mips) FTexture2DMipMap();

	Mip->SizeX = _width;
	Mip->SizeY = _height;
	Mip->SizeZ = _depth;

	// Copying data
	Mip->BulkData.Lock(LOCK_READ_WRITE);
	void* MipData = Mip->BulkData.Realloc(TotalSize);
	FMemory::Memcpy(MipData, &data, TotalSize);
	Mip->BulkData.Unlock();

#if WITH_EDITORONLY_DATA
	_volumeTexture->MipGenSettings = TMGS_LeaveExistingMips;
	_volumeTexture->Source.Init(_width, _height, _depth, 1, ETextureSourceFormat::TSF_RGBA8, data);
#endif
	
	_volumeTexture->UpdateResource();
	Package->MarkPackageDirty();
	FAssetRegistryModule::AssetCreated(_volumeTexture);

	FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension());
	bool bSaved = UPackage::SavePackage(Package, _volumeTexture, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, *PackageFileName, GError, nullptr, true, true, SAVE_NoError);

	// Then generating the material
	this->createMaterial(name);
}

```

The problem is that when I execute the code, I get the following error:

```
[2019.07.03-08.31.35:961][519]LogWindows: Error: === Critical error: ===
[2019.07.03-08.31.35:961][519]LogWindows: Error: 
[2019.07.03-08.31.35:961][519]LogWindows: Error: Fatal error!
[2019.07.03-08.31.35:961][519]LogWindows: Error: 
[2019.07.03-08.31.35:961][519]LogWindows: Error: Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0xdf0d0000

```

And then the logs indicate that the crash happens at this line:

```
Mip->BulkData.Unlock();

```

However, if I try to print a UE\_LOG just before this line, it doesn’t appear. Which strongly makes me think that the actual error comes from the Memcpy:

```
FMemory::Memcpy(MipData, &data, TotalSize);

```

By testing, I found out that there is a problem with the dimensions of my texture. The file I work with most is 100x100x60, but I managed to execute the code without errors by passing 18x18x18 (or less, like 16x16x16) as parameters to my function and I have no idea why this works.

Any ideas ?

---

<div class="post-metadata">

**Author:** ![superdreamgen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@superdreamgen](https://forums.unrealengine.com/u/superdreamgen)\
**Post date:** [August 6, 2019, 5:38am UTC](https://forums.unrealengine.com/t/editor-crashes-on-memcpy/451638/2 "2019-08-06T05:38:22Z")

</div>

Have you found a solution to this? I’m getting a crash on BulkData.Unlock() after trying to Memcpy to a UTexture2D.

---

<div class="post-metadata">

**Author:** ![Aseptio](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Aseptio](https://forums.unrealengine.com/u/Aseptio)\
**Post date:** [August 6, 2019, 8:51am UTC](https://forums.unrealengine.com/t/editor-crashes-on-memcpy/451638/3 "2019-08-06T08:51:23Z")

</div>

Yes, I was actually dereferencing my data pointer…

```
FMemory::Memcpy(MipData, &data, TotalSize);
FMemory::Memcpy(MipData, data, TotalSize);

```

After this, I still had some crashes… turns out you may need to delete the data at the end of your function:

```
delete[] data;

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![superdreamgen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@superdreamgen](https://forums.unrealengine.com/u/superdreamgen)\
**Post date:** [August 9, 2019, 7:55pm UTC](https://forums.unrealengine.com/t/editor-crashes-on-memcpy/451638/4 "2019-08-09T19:55:13Z")

</div>

Thank you! I will try this.
