[Question] Memory access error

Hey guys,

I’m working on making a Slate listview object and finally got it to compile, only to have a memory access issue once it starts.

This is the error:

Here’s the widget code:

#include "TestProject1800.h"
#include "SMyGameSlateHUDWidget.h"

void SMyGameSlateHUDWidget::Construct(const FArguments& InArgs)
{
	OwnerHUD = InArgs._OwnerHUD;

	TArray> Items = GetListOfItems();
	

	
	ChildSlot
	.VAlign(VAlign_Fill)
	.HAlign(HAlign_Fill)
	[

			SNew(SScrollBox)
			+SScrollBox::Slot() .Padding(10,5)
			[
				
					SNew(SListView>)
					.ItemHeight(24)
					.ListItemsSource(&Items)
					.OnGenerateRow(this, &SMyGameSlateHUDWidget::OnGenerateWidgetForList)
					
				
			]
			

			
			
	];

	Items.Empty();
		
}

TSharedRef SMyGameSlateHUDWidget::OnGenerateWidgetForList(TSharedPtr InItem, const TSharedRef& OwnerTable)
{

	STextBlock content;
	content.SetText(FString::Printf(TEXT("test string")));
	
	TSharedRef sptrContent(&content);

	TSharedRef>> test = SNew(STableRow>,OwnerTable);

	test->SetContent(sptrContent);
	
	return test;

}


FString SMyGameSlateHUDWidget::GetSomeString() const
{
	return FString(TEXT("This is a first test."));
}

FString SMyGameSlateHUDWidget::GetSomeOtherString() const
{
	return FString(TEXT("This is a second test."));
}

TArray> SMyGameSlateHUDWidget::GetListOfItems() const
{
	FString* temp = new FString(TEXT("TestText"));
	TSharedPtr temp2 = TSharedPtr(&temp);
	FString* tempB = new FString(TEXT("TestText2"));
	TSharedPtr temp2B = TSharedPtr(&tempB);
	TArray> Items = TArray>();
	Items.Add(temp2);
	Items.Add(temp2B);

	return Items;
}

FMargin SMyGameSlateHUDWidget::GetPadding() const
{
	float xLoc = 100;
	float yLoc = 100;
	if ( GEngine && GEngine->GameViewport )
	{
		FVector2D ViewportSize;
		GEngine->GameViewport->GetViewportSize(ViewportSize);
		//GEngome->Get
		//FVector2D testLoc = FVector2D(temp);
		//xLoc = testLoc.X;
		//yLoc = testLoc.Y;
		xLoc = ViewportSize.X*0.3;
		yLoc = ViewportSize.Y*0.4;
	}
	return FMargin(xLoc,yLoc,0,0);
}

Here’s the header:

#pragma once


#include "Slate.h"

class SMyGameSlateHUDWidget : public SCompoundWidget
{
	SLATE_BEGIN_ARGS(SMyGameSlateHUDWidget)
		: _OwnerHUD()
	{}

	SLATE_ARGUMENT(TWeakObjectPtr, OwnerHUD)

	SLATE_END_ARGS()

	/** Needed for every widget */
	void Construct(const FArguments& InArgs);

	/** returns a string of information to display */
	FString	GetSomeString() const;

	/** returns a string of information to display */
	FString	GetSomeOtherString() const;

	/** returns the padding of the object */
	FMargin	GetPadding() const;

	
	TArray> SMyGameSlateHUDWidget::GetListOfItems() const;

	TSharedRef SMyGameSlateHUDWidget::OnGenerateWidgetForList(TSharedPtr InItem, const TSharedRef& OwnerTable);


	
protected:
	/** Pointer to our parent HUD */
	TWeakObjectPtr OwnerHUD;
};

It doesn’t happen every single time, but 90% or so.
Not sure how diagnosable it is, but any help would be greatly appreciated :slight_smile:

I am not saying I know exactly what the issue is,

but I can highly recommend you try the following

Try changing from this

TArray> SMyGameSlateHUDWidget::GetListOfItems() const
{
    FString* temp = new FString(TEXT("TestText"));
    TSharedPtr temp2 = TSharedPtr(&temp);
    FString* tempB = new FString(TEXT("TestText2"));
    TSharedPtr temp2B = TSharedPtr(&tempB);
    TArray> Items = TArray>();
    Items.Add(temp2);
    Items.Add(temp2B);
 
    return Items;
}

to this

:slight_smile:

void SMyGameSlateHUDWidget::GetListOfItems(TArray>& ListIn) const
{
	ListIn.Empty();
    ListIn.Add(MakeShareable(new FString("TestText")));
    ListIn.Add(MakeShareable(new FString("TestText2")));
}

you need to change this

{
    OwnerHUD = InArgs._OwnerHUD;
 
    TArray> Items = GetListOfItems();

to this

{
    OwnerHUD = InArgs._OwnerHUD;
 
    TArray> Items; 
	GetListOfItems(Items);

and your header:

TArray> SMyGameSlateHUDWidget::GetListOfItems() const;

to this

void GetListOfItems(TArray>& ListIn) const;

#MakeShareable

The critical part is the use of MakeShareable + the new operator

I see this everywhere in the .h files.

it also means you have to write less code using the method I show above

I changed your array to be passed in by reference instead of returned.

You can look up “pass by reference” for more info :slight_smile:

#Enjoy!

Enjoy!

Rama