use win32 to allocate memory (more stable)

This commit is contained in:
Akash Mozumdar 2018-10-07 11:02:00 -04:00
parent e126e4c4f3
commit a21ee5bba4
2 changed files with 6 additions and 6 deletions

View File

@ -28,7 +28,7 @@ std::map<int, QString> LoadExtensions()
bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo) bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo)
{ {
bool success = true; bool success = true;
wchar_t* sentenceBuffer = (wchar_t*)malloc((sentence.size() + 1) * sizeof(wchar_t)); wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t));
wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str()); wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str());
InfoForExtension* miscInfoLinkedList = new InfoForExtension; InfoForExtension* miscInfoLinkedList = new InfoForExtension;
InfoForExtension* miscInfoTraverser = miscInfoLinkedList; InfoForExtension* miscInfoTraverser = miscInfoLinkedList;
@ -40,12 +40,12 @@ bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std
{ {
wchar_t* prev = sentenceBuffer; wchar_t* prev = sentenceBuffer;
sentenceBuffer = i.second(sentenceBuffer, miscInfoLinkedList); sentenceBuffer = i.second(sentenceBuffer, miscInfoLinkedList);
if (sentenceBuffer != prev) free((void*)prev); if (sentenceBuffer != prev) HeapFree(GetProcessHeap(), 0, prev);
} }
} }
catch (...) { success = false; } catch (...) { success = false; }
sentence = std::wstring(sentenceBuffer); sentence = std::wstring(sentenceBuffer);
free((void*)sentenceBuffer); HeapFree(GetProcessHeap(), 0, sentenceBuffer);
delete miscInfoLinkedList; delete miscInfoLinkedList;
return success; return success;
} }

View File

@ -19,7 +19,7 @@ struct SentenceInfo
int64_t operator[](std::string propertyName) int64_t operator[](std::string propertyName)
{ {
for (auto i = list; i != nullptr; i = i->next) if (propertyName == i->name) return i->value; for (auto i = list; i != nullptr; i = i->next) if (propertyName == i->name) return i->value;
return 0; throw;
} }
}; };
@ -29,7 +29,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
* You shouldn't mess with this or even look at it unless you're certain you know what you're doing. * You shouldn't mess with this or even look at it unless you're certain you know what you're doing.
* Param sentence: pointer to sentence received by Textractor (UTF-16). * Param sentence: pointer to sentence received by Textractor (UTF-16).
* You should not modify this sentence. If you want Textractor to receive a modified sentence, copy it into your own buffer and return that. * You should not modify this sentence. If you want Textractor to receive a modified sentence, copy it into your own buffer and return that.
* Please allocate the buffer using malloc() and not new[] or something else: Textractor uses free() to free it. * Please allocate the buffer using HeapAlloc() and not new[] or malloc() or something else: Textractor uses HeapFree() to free it.
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence. * Param miscInfo: pointer to start of singly linked list containing misc info about the sentence.
* Return value: pointer to sentence Textractor takes for future processing and display. * Return value: pointer to sentence Textractor takes for future processing and display.
* Return 'sentence' unless you created a new sentence/buffer as mentioned above. * Return 'sentence' unless you created a new sentence/buffer as mentioned above.
@ -42,7 +42,7 @@ extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sen
if (ProcessSentence(sentence, SentenceInfo{ miscInfo })) if (ProcessSentence(sentence, SentenceInfo{ miscInfo }))
{ {
// No need to worry about freeing this: Textractor does it for you. // No need to worry about freeing this: Textractor does it for you.
wchar_t* newSentence = (wchar_t*)malloc((sentence.size() + 1) * sizeof(wchar_t*)); wchar_t* newSentence = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t));
wcscpy_s(newSentence, sentence.size() + 1, sentence.c_str()); wcscpy_s(newSentence, sentence.size() + 1, sentence.c_str());
return newSentence; return newSentence;
} }