a heap has been corrupted

I didn’t find a complete solution, but I changed the setData’s first argument from std::string to const char* and added a second argument for its size…

Hi everyone,
I’m developing a plugin for UE4. I have developed a framework to communicate with multiple nodes in a network and interact to create a smart environment. The system works both as a standalone application and as included as a library in an other application. The problem is in UE4. The organization is as follows:

Plugin:

  • A ThirdParty library ← my framework and its dependencies
  • A source ← an ActorComponent that wraps functions of my framework

the plugin is successfully created.

UE4 classes:

I added another ActorComponent that uses the plugin and that ActorComponent. In particular I send an FString to the ActorComponent and then to my framework.

The ActorComponent in the plugin has this method to send the message:

void UPhaserComponent::sendMessage(FString msg) {
	phaser_ts_t now = Utils::getCurrentTimestampInMillis();
	std::string value;
	value = TCHAR_TO_ANSI(*msg);

	UE_LOG(LogTemp, Error, TEXT("before set data\n"));
	gid->setData(value, now - 300, now);
	//gid->setData("recipes with pasta", now - 300, now);
	UE_LOG(LogTemp, Error, TEXT("after set data\n"));
}

and the following method in my framework receives the message. However it is compiled. data there is dynamically allocated in the constructor.

void GenericInputLabel::setData(std::string newdata, phaser_ts_t start, phaser_ts_t end) {
	if (newdata.size() > MAX_BUFFER_SIZE) // 512
		newdata = newdata.substr(0, 5);
	std::memcpy(data, newdata.c_str(), newdata.size());
	data[newdata.size()] = '\0';

	this->start = start;
	this->end = end;

	setNewData(true);
}

but I receive an “unhandled exception in … ntdll.dll. A heap has been corrupted” exception, after setNewData(true) (yes on the brace) and I don’t know why.

Now, each system throws that exception as it detects an heap invalid access, but you don’t know exactly when it happens, but it never happens with short messages like “hello”, but always with “bigger” messages as “recipes with pasta”. I tried also to avoid conversions and statically send the same message, but I get always the same crash. However if I ignore that exception, continuing with the debugging, UE4 does not crash and successfully sends the message!

Please, save me!

where is your “data” variable declared? how are you resetting/setting its size? std::string::c_str already gives a null terminated string…

data is declared in the .h and defined in the constructor:

GenericInputLabel::GenericInputLabel(const char* name) {
	genericType = T_LABEL;
	inputDataName = name;
	channel = -1;
	newData = false;
	open_ = false;

	mmi = MultiModalInput::getInstance();
	start = end = 0;
	data = new char[MAX_BUFFER_SIZE]; //512
}

I prefer to replace just its content, without changing its size

why are you limiting your char length?.. right now you are hitting a null pointer while reading your memcopied string before your max size limit is reached. this is causing your heap to corrupt and a crash… if you need pre-defined strings, why not use a enum? if not, then you did not think of situations that your input might be utf8 or even utf16…

I tried with normal std::string, but it was crashing :slight_smile: However it seems that the problem was in passing string as an argument, so I can now replace char data* with string. I can’t use pre-defined strings. It was just a test. That string is a user’s input, so it needs to be free