c - 未分配正在释放的指针,中止陷阱 : 6

标签 c pointers malloc free

我不精通 C 编程,所以如果这不是一个强有力的问题,请原谅我。在下面的代码中,我只能在获取nsamplepts的值后为samplesVec分配内存,但我需要将 vector samplesVec返回给main 供进一步使用(尚未编码)。但是,我收到以下错误:

终端窗口中出现错误: ImportSweeps(3497,0x7fff7b129310) malloc: * 对象 0x7fdaa0c03af8 错误: 未分配正在释放的指针 *在malloc_error_break中设置断点进行调试 中止陷阱:6

我正在使用 Mac OS X Mavericks 和 gcc 编译器。感谢您的帮助。

*已编辑!!!经过评论者的宝贵意见后,以下内容代表了原始问题的解决方案(不再可用)*

下面的代码修改似乎解决了我原来的问题。感谢大家提出的宝贵意见!

/* Header Files */
#define LIBAIFF_NOCOMPAT 1 // do not use LibAiff 2 API compatibility
#include <libaiff/libaiff.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <math.h>

/* Function Declarations */
void FileSearch(char*, char*, char*, char*, char*);
int32_t *ImportSweeps(char*);

/* Main */
int main()
{
char flag1[2] = "N";
char binname[20] = "bin1"; // dummy assignment
char buildfilename[40] = "SweepR";
char skeletonpath[100] = "/Users/.../Folder name/";
int k, len;

/* Find the sweep to be imported in the directory given by filepath */
FileSearch(skeletonpath, binname, buildfilename, skeletonpath, flag1);
if (strcmp(flag1,"Y")) {
    printf("No file found. End of program.\n");
} else {
    len = (int) strlen(skeletonpath);
    char *filepath = malloc(len);
    for (k = 0; k < len; k++) {
        filepath[k] = skeletonpath[k];
    }
    printf("File found! Filepath: %s\n", filepath);
    // Proceed to import sweep
    int32_t *sweepRfile = ImportSweeps(filepath);
    if (sweepRfile) {
        printf("Success!\n");
        // Do other things with sweepRfile
        free(sweepRfile);
    }
    free(filepath);
}
return 0;
}

/* Sub-Routines */
void FileSearch(char *dir, char *binname, char *buildfilename, char* filepath, char* flag1)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
    fprintf(stderr,"Cannot open directory: %s\n", dir);
    return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
    lstat(entry->d_name, &statbuf);
    if(S_ISDIR(statbuf.st_mode)) {
        /* Found a directory, but ignore . and .. */
        if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
            continue;
        strcpy(binname,entry->d_name);
        strcpy(buildfilename,"SweepR");
        /* Recurse at a new indent level */
        FileSearch(entry->d_name, binname, buildfilename, filepath, flag1);
    }
    else {
        sprintf(buildfilename, "%s%s.aiff", buildfilename, binname);
        if (strcmp(entry->d_name,buildfilename)) {
            strcpy(buildfilename,"SweepR");
        } else {
            sprintf(filepath, "%s%s/%s", filepath, binname, buildfilename);
            strcpy(flag1,"Y");
            break;
        }
    }
}
chdir("..");
closedir(dp);
}


int32_t *ImportSweeps(char *filepath)
{
char *filepathread = filepath;

/* Initialize files for importing */
AIFF_Ref fileref;

/* Intialize files for getting information about AIFF file */
uint64_t nSamples;
int32_t *samples = NULL;
int32_t *samplesVec = NULL;
int channels, bitsPerSample, segmentSize, ghost, nsamplepts;
double samplingRate;

/* Import Routine */
fileref = AIFF_OpenFile(filepathread, F_RDONLY) ;
if(fileref)
{
    // File opened successfully. Proceed.
    ghost = AIFF_GetAudioFormat(fileref, &nSamples, &channels, &samplingRate, &bitsPerSample, &segmentSize);
    if (ghost < 1)
    {
        printf("Error getting audio format.\n");
        AIFF_CloseFile(fileref); return (int32_t) 0;
    }
    nsamplepts = ((int) nSamples)*channels;
    samples = malloc(nsamplepts * sizeof(int32_t));
    samplesVec = malloc(nsamplepts * sizeof(int32_t));
    ghost = AIFF_ReadSamples32Bit(fileref, samples, nsamplepts);
    if (ghost) {
        for (int k = 0; k < nsamplepts; k++) {
            samplesVec[k] = *(samples+k);
        }
    }
    free(samples);
    AIFF_CloseFile(fileref);
}
return samplesVec;
}

最佳答案

所以...据我所知...:-)

samplesVec,如果fileref为 false,则 ImportSweeps 的返回值未初始化。如果 samplesVec 未显式初始化,自动(== 本地)变量无法保证其值 - 换句话说 samplesVec 可以携带任何地址。如果 samplesVec 幸运地不是 NULL(另一方面可能经常出现这种情况),您可以尝试释放未分配的内存垃圾,或者运气不好,在其他地方分配了一个。

如果我的猜测是正确的,您可以通过以下方式轻松解决此问题:

int32_t *samples;
int32_t *samplesVec = NULL;

无论如何,如果您不在下一行中使用它,那么尽快使用一些有意义的错误或虚拟值初始化任何变量是一个好主意。由于指针是可怕的野兽,如果我不在声明时使用有用的值初始化它们,我总是NULL它们。

编辑:为了可读的英语近似值进行了一些小的改动。 :-)

关于c - 未分配正在释放的指针,中止陷阱 : 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22163219/

相关文章:

c - 为什么带星号和不带星号的指针变量在 printf 中表现不同?

c - 循环中的 malloc 和 free。对象可能在释放后被修改。?

c - malloc 和 calloc

c++ - 免费(指针)方法的性能?

c - 移动 n 位的 __m128i

c - 为什么我的线程没有正确生成? C

c - 我被 C 中的输出困住了(多维数组运算)

c将指针传递给递归函数

c - 从文件中读取c代码

c - 通过glib队列传递数据结构