dimanche 3 avril 2022

Delete dynamic allocated memory from std:queue [closed]

class Queue
{
 std::queue<unsigned char *> QueueReceiver;
 void sendtoRes(unsigned char *message,int Receiver);
}
void Queue::sendtoRes(unsigned char *message,int Receiver)
{
 unsigned char *object= new unsigned char[Receiver];
 if(message)
 {
  QueueReceiver.push(object);
 }
}

Now Google Test for it

TEST(UTQueue,sendtoRes)
{
  unsigned char message[80]={0x50,0x54,0x32};
  int len = 80;
  Queue *Testobj;
  Testobj->sendtoRes.push(message,len);
}

I need to write another GTest case for solving leaks problem, here I have to delete dynamic allocated memory which is hold by queue. I am unable to release memory from queue. I am writing like this

TEST(UTQueue,sendtoRes)
{
 Queue *Testobj;
 while(!Testobj->QueueReceiver.empty())
 {
  delete Testobj->QueueReceiver.front(); 
  Testobj->QueueReceiver.pop();
 }
}

by writing this, there is no effect in leaks. how can I solve this issue ?

Aucun commentaire:

Enregistrer un commentaire