在运行代码时,我得到以下错误:ds(57203,0x70000fba3000)malloc: *对象0x7ff875402848的错误:释放对象的校验和不正确-对象在释放后可能已被修改。 * 在malloc_error_break中设置一个断点进行调试
有时它可以工作,有时在尝试分配一个新节点后会崩溃(请参见createNode函数),所以我怀疑错误是从那里来的。
我在做什么错?我该如何解决?
我尝试调试代码并更改几个malloc,但无法解决问题。
正如我之前所说,我怀疑该错误是在createNode函数中。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
void* threadFunction(void* searchTerm);
void scanDirName(char * path, char * searchTerm);
char* rootSD;
pthread_mutex_t qlock;
struct Node {
char* data;
struct Node* next;
};
// Two glboal variables to store address of front and rear nodes.
struct Node* front = NULL;
struct Node* rear = NULL;
// To Enqueue an integer
void Enqueue(char* x) {
pthread_mutex_lock(&qlock);
/* printf("\nhere\n");*/
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data =x;
temp->next = NULL;
if(front == NULL && rear == NULL){
front = rear = temp;
pthread_mutex_unlock(&qlock);
return;
}
rear->next = temp;
rear = temp;
pthread_mutex_unlock(&qlock);
}
// To Dequeue an integer.
char* Dequeue() {
pthread_mutex_lock(&qlock);
struct Node* temp = front;
if(front == NULL) {
pthread_mutex_unlock(&qlock);
return NULL;
}
char* data;
data = front->data;
if(front == rear) {
front = rear = NULL;
}
else {
front = front->next;
}
free(temp);
pthread_mutex_unlock(&qlock);
return data;
}
void Print() {
struct Node* temp = front;
while(temp != NULL) {
printf("%s ",temp->data);
temp = temp->next;
}
printf("\n");
}
void* threadFunction(void* st){
char* filepath;
filepath = NULL;
char* searchTerm;
searchTerm = (char*)st;
while (filepath == NULL) {
filepath = Dequeue();
}
printf("about to enter with %s, %s\n",filepath, searchTerm);
fflush(stdout);
scanDirName(filepath, searchTerm);
if (strcmp(filepath,rootSD) != 0)
free(filepath);
return (void*)1;
}
void scanDirName(char * path, char * searchTerm){
DIR * d = opendir(path); // open the path
char* str3;
if(d==NULL) return; // if was not able return
;
struct dirent * dir; // for the directory entries
while ((dir = readdir(d)) != NULL) // if we were able to read somehting from the directory
{
if(dir-> d_type == DT_DIR){ //
if (dir->d_type == DT_DIR && strcmp(dir->d_name, ".") != 0 & strcmp(dir->d_name, "..") != 0) // if it is a directory
{
str3 = malloc(1+strlen("/") + strlen(searchTerm)+ strlen(dir->d_name) );
if (!str3){
return;
}
strcpy(str3, path);
strcat(str3, "/");
strcat(str3, dir->d_name);
printf("\n---\n%s\n---\n",str3);
Enqueue(str3);
printf("Succ");
}
} else if(dir-> d_type == DT_REG){ //
if(strstr(dir->d_name, searchTerm)){
printf("%s/%s\n", path, dir->d_name);
}
}
}
closedir(d); // finally close the directory
}
int main(int argc, char* argv[]){
if (argc != 4){
printf("ERROR\n");
exit(1);
}
char* rootSearchDir = argv[1];
char* searchTerm = argv[2];
int threadsNumber = atoi(argv[3]);
pthread_t threadsCollection[threadsNumber];
rootSD = rootSearchDir;
Enqueue(rootSearchDir);
int i;
for (i=0; i<threadsNumber; i++){
if(pthread_create(&threadsCollection[i], NULL, threadFunction, (void*)searchTerm)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
}
int rc;
for (i=0; i<threadsNumber; i++){
rc = pthread_join((threadsCollection[i]), NULL);
if(rc) {
fprintf(stderr, "Error joining thread, %d\n", rc);
return 1;
}
}
}
}
此代码使用线程从根搜索目录开始搜索名称包含searchTerm的文件。
答案 0 :(得分:0)
问题是您正在分配TextView
的大小,但正在复制searchTerm
。
path
的长度和path
的长度相同的机会较小。因此,searchTerm
的访问权限超出范围,并调用未定义的行为。
str3
要解决的是分配长度为 str3 = malloc(1+strlen("/") + strlen(searchTerm)+ strlen(dir->d_name) );
if (!str3){
return;
}
strcpy(str3, path); //Here
strcat(str3, "/");
strcat(str3, dir->d_name);
的内存。
path