0
これは、valgrindのmemcheckの出力をデモンストレーションし、レビューするのに役立つ小さなテストです。誰かが、リストの途中からノードを削除して解放する方法を理解する手助けができますか?私がメモリ節約をしていることをmemcheckが教えてくれていますが、もし私がそこに置いておくと、ループ。この謎を解消する方法はありますか?valgrindエラーのないリストの途中で削除されたノードを解放するには?
あなたのループを見てみるとTEST(UtilityGeneralUnittest, valgrindTests)
{
//declare a node type
typedef struct node{
int size;
int value;
struct node *next;
pthread_mutex_t *lock;
}node_t;
//make the head
node_t *head;
head = (node_t*)malloc(1 * sizeof(node_t));
head->size = 0;
head->next = NULL;
head->lock = (pthread_mutex_t*)malloc(1 * sizeof(pthread_mutex_t));
pthread_mutex_init(head->lock, NULL);
//create array for storing values
int array[10];
//build a list with random numbers
for (int i = 0; i < 10; i++)
{
node_t *newNode;
newNode = (node_t*)malloc(1 * sizeof(node_t));
newNode->value = rand() % 100 + 1;
newNode->next = NULL;
newNode->lock = (pthread_mutex_t*) malloc(1 * sizeof(pthread_mutex_t));
pthread_mutex_init(newNode->lock, NULL);
array[i] = newNode->value;
if (head->next == NULL)
{
head->next = newNode;
head->size++;
}
else
{
node_t *tmp = head->next;
head->next = newNode;
newNode->next = tmp;
head->size++;
}
}
// assert the list added nodes
ASSERT_EQ(10, head->size);
//sanity check; print the list
node_t *printer = head;
while(printer->next != NULL)
{
printer = printer->next;
std::cout << "value: ";
std::cout << printer->value << ", ";
}
std::cout << "\n";
// the meat and potatoes: deleting with locks.
int removeMe = array[rand() % 10];
bool verifyDel = true;
int checkVal = removeMe;
node_t *prev;
node_t *cur;
prev = head;
pthread_mutex_lock(prev->lock);
while((cur = prev->next) != NULL) //******** this is the problem
{
pthread_mutex_lock(cur->lock);
if(cur->value == removeMe)
{
prev->next = cur->next;
pthread_mutex_unlock(cur->lock);
pthread_mutex_unlock(prev->lock);
cur->next = NULL;
head->size--;
free(cur->lock); ///******** this is the problem
free(cur); ///****** this is the problem
}
pthread_mutex_unlock(prev->lock);
prev = cur;
}
//pthread_mutex_unlock(prev->lock);
//verify node has been removed
printer = head;
while(printer->next != NULL)
{
printer = printer->next;
if(printer->value == checkVal)
{
verifyDel = false;
}
std::cout << "value: ";
std::cout << printer->value << ", ";
}
std::cout << "\n";
ASSERT_TRUE(verifyDel);
//clean up: delete the list
while((printer = head) != NULL)
{
head = head->next;
free(printer->lock);
free(printer);
std::cout << "free!!!" << std::endl;
}
}
をそして、あなたはあなたのミューテックスあなたがそれらをロックするよりも、複数回のロックを解除するように見えるが... –
@TobySpeightはい、これはgTestのテストケースです。私はあなたが関数の肉をコピーしてmain()にコピーすることができ、コンパイルして実行することができると思います。 malloc()の使用は、Cのプログラマがハードに死んでいく日です。私はあなたの提案を試して、その課題の周りにテストを入れます。あなたに知らせる! –
@TobySpeightあなたが正しかった、それはその割り当てでした。課題の周りにテストケースを追加するのではなく、私の答えを見て続行しました。 –