Delete takes off when trying to delete a structure

There is a function:

// Заполнение очереди комманд void QueleСommandFiller(...) { struct MyMessen* mes; ... // Получение сообщения mes = GetMsg(responsSocket); // Анализ сообщения (Lasciate ogni speranza, voi ch'entrate) if (mes != NULL) { CM_Add(mes, cm); delete(mes); } } } 

Please pay attention to the function:

 // Получение сообщения struct MyMessen* GetMsg(void* responsSocket) { zmq_msg_t message; zmq_msg_init(&message); if (zmq_msg_recv(&message, responsSocket, ZMQ_DONTWAIT) == -1) { zmq_msg_close(&message); return NULL; } struct MyMessen* mes = (struct MyMessen*) zmq_msg_data(&message); zmq_msg_close(&message); return mes; } 

AND

 // Добавление элементов в очередь void CM_Add(struct MyMessen* mes, struct CommandQueue* cm) { struct CommandQueue_leaf tmp; tmp.mes = *mes; tmp.status = 0; cm->mes.push_back(tmp); } 

When you call delete mes is not null, checked on the debugger.

But still flies to

 extern "C" int __cdecl _CrtIsValidHeapPointer(void const* const block) { if (!block) return FALSE; return HeapValidate(__acrt_heap, 0, header_from_block(block)); } 

PS I look at the lines with suspicion:

struct MyMessen * mes = (struct MyMessen *) zmq_msg_data (& message);

and

cm-> mes.push_back (tmp);

  • And why are you trying to remove a structure that was not selected? - VTT

1 answer 1

You are trying to delete for something that was not selected. The result is expected. Where did you get the idea to delete here?

Moreover, according to the description of zmq_msg_data , it does not allocate any memory at all, but simply returns a pointer to some resources associated with the zmq_msg_t message object. Since zmq_msg_t message is a local variable in GetMsg , and besides, all associated resources are freed by calling zmq_msg_close , there is no question of returning the pointer mes out of GetMsg - this is a "dead" pointer.