What Do I Do If an Exception Occurs Because a Smart Pointer Is Used to Manually Free Memory but the Destructor Is Not HIAI_DFree?
Symptom
HIAI_DMalloc or HIAIMemory::HIAI_DMalloc is called to allocate memory for host-device or device-host data transmission. flag is set to MEMORY_ATTR_MANUAL_FREE to manually free memory. A smart pointer is used to store the address of the allocated memory. If the destructor of the smart pointer is incorrect, the program becomes abnormal and memory leakage occurs.
Solution
Set the destructor of the smart pointer to HIAI_DFree or HIAIMemory::HIAI_DFree.
An example of the code is as follows:
/** * @ Allocates memory to be manually freed. If a smart pointer is used, use HIAI_DFree as the destructor. **/ int main() { // Uses DMalloc to manually free memory. unsigned char* inbuf = nullptr; uint32_t bufferLen = 1080*1920; HIAI_StatusT getRet = hiai::HIAIMemory::HIAI_DMalloc(bufferLen, (void*&)inbuf, 10000,hiai::HIAI_MEMORY_ATTR::MEMORY_ATTR_MANUAL_FREE); // getRet exception judgment // Stores the data to be transmitted in inbuf. The size of the data is bufferLen. // Assembles the engineTranData structure for sending data EngineTransNewT engineTranData; engineTranData->bufferSize = bufferLen; engineTranData->transBuff.reset(std::static_pointer_cast<uint8_t *> (inbuf), [](uint8_t *)(addr ) { // Uses HIAI_DFree as the destructor. Otherwise, memory leakage occurs. hiai::HIAIMemory::HIAI_DFree(void * addr) }); // Sends data to the peer end by calling SendData. HIAI_StatusT sendRet = SendData(DEFAULT_PORT,"EngineTransNewT",std::static_pointer_cast<void>(engineTranData)); // sendRet exception judgment }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot