x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

a heap has been corrupted

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!

Product Version: UE 4.12
Tags:
more ▼

asked Jan 18 '17 at 10:51 AM in C++ Programming

avatar image

Dario Di Mauro
6 1 2 5

avatar image emrahgunduz Jan 18 '17 at 11:20 AM

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

avatar image Dario Di Mauro Jan 18 '17 at 11:25 AM

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

avatar image emrahgunduz Jan 18 '17 at 12:03 PM

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...

avatar image Dario Di Mauro Jan 18 '17 at 02:56 PM

I tried with normal std::string, but it was crashing :) 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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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...

more ▼

answered Jan 18 '17 at 11:54 AM

avatar image

Dario Di Mauro
6 1 2 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question