Calling UInterface-implementation causes access violation

Hi there,
first of all, excuse any mistake in posting since its my first post.

I am working on an implementation which allows me to send any data (and also size) from server to client. For that purpose i defined an interface in C++, in which i define the fundamentals like receiving and providing data. The whole networking is done in the class AMimiNetLoader.

My interface:



#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "MimiNetLoadable.generated.h"

class AMimiNetLoader;

UINTERFACE(MinimalAPI)
class UMimiNetLoadable : public UInterface
{
  GENERATED_BODY()
};

class MIMITD_API IMimiNetLoadable
{
  GENERATED_BODY()

public:
  virtual void NetLoaderAvailable(AMimiNetLoader* RefNetLoader) = 0;
  virtual void NetLoaderDataAvailable(const uint8* Data,
  const uint32 Size) = 0;
  virtual void NetLoaderDataRequested(uint8* Data, const uint32 Size,
  uint32* BytesWritten) = 0;
  virtual uint32 NetLoaderAcceptedPacketTypes() const = 0;
};


My class implementing the interface:



class MIMITD_API AMimiTileMap : public APaperTileMapActor, public IMimiNetLoadable


and the CPP side of that class (of course i implemented all other pure virtual methods aswell):



void AMimiTileMap::NetLoaderAvailable(AMimiNetLoader* RefNetLoader)
{
  // request tilemap if you are a client
  if(GetLocalRole() == ROLE_AutonomousProxy)
  RefNetLoader->RequestData(this);
}


Now: MimiNetLoader is created in GameMode::PostLogin(…). Its ownership is then set to the PlayerController which conntected and in MimiNetLoader::BeginPlay() it gets all Actors implementing my interface. I then proceed to call IMimiNetLoadable::NetLoaderAvailable(this), which causes an accessviolation.



void AMimiNetLoader::BeginPlay()
{
  Super::BeginPlay();

  UGameplayStatics::GetAllActorsWithInterface(GetWorld(),
  UMimiNetLoadable::StaticClass(), NetLoadables);

  for (int32 i = 0; i < NetLoadables.Num(); ++i)
  {
    // NetLoadables is a member of AMimiNetLoader of type TArray<AActor*>
    IMimiNetLoadable* Loadable
    = reinterpret_cast<IMimiNetLoadable*>(NetLoadables*);
    // with the debugger i could determine that Loadable was indeed my TileMap implementation

    Loadable->NetLoaderAvailable(this); // this line causes an access violation, trying to access 0x0...0004
  }
}


Now, i’m not sure if it is caused by me doing stuff in PostLogin or by me doing something wrong with the interface. It seems the error occurs within unreals dlls and thus i can’t really determine whats going wrong.

Any ideas?

reinterpret_cast is hella-dangerous - use the engines’ built-in Cast<>() function. This will work so long as the interface is native only, and so long as you have no Blueprint Event functions. You should probably add the following to the UINTERFACE() macro, as it will prevent you from being able to inherit in Blueprint classes:



meta = (CannotImplementInterfaceInBlueprint)


If you want to make a blueprint-implementable interface, then you can’t use cast functions - they have to be called through reflection.


Putting all of this aside, Unreal has a perfectly good networking system for sending data from Server->Client, what’s the reason for not using that in this case?

Thank you for your fast answer.

Unfortunately I’m writing from work and can’t try your correction, but I will as soon as I get the opportunity.

The reason I started building this system is because of the size limitations of rpc’s. It allows me to send up to 2^12 elements in a TArray and a maximum of 2^16 bytes. I know there is a setting which I can change to send up to 2^16 elements in a TArray but that still won’t help me enough.

I am actually hoping that you are referring to a different way to send data, because I’d change in an instant. I’d like to build a game in which players can build their own tilemaps and share those via the network in a lobby or something.

So please tell me there is a built-in system for that :smiley:

Well generally speaking, Unreals networking architecture is geared more towards delivering lots of small packets quickly, rather than huge chunks of data.

You could still do what you need to do through RPC’s by “chunking” the data yourself, but it’s probably not something you want to be doing all the time either way. Depends on the game really…

That’s actually exactly what my NetLoader is doing…
I found an interesting article about TCP and UDP sockets in the old wiki. If I run into more problems I might do the map distribution that way

This did the trick

I’m sometimes really confused by unreal

Thank you for your help