KERN_PROTECTION_FAILURE in C

Discussion in 'Mac Programming' started by farmerdoug, Apr 29, 2009.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    The code compiles but I get a Kern_Protection_Failure and fails at the fread.
    Suggestions? Thanks

    typedef struct matchedfilter{
    int col;
    int row;
    float ** filters;
    } matchedfilter;



    struct dirent *filterfile;
    DIR *ld;
    matchedfilter *mfilters;
    int filtnum = 0;
    FILE * fp;
    char * filename;

    ld = (DIR *) calloc (1, sizeof(DIR));
    mfilters = (matchedfilter *)calloc(LENSLETS_SIZE*LENSLETS_SIZE, sizeof(matchedfilter));
    filename = (char*) calloc(25, sizeof(char));
    ld = opendir(filterdir);

    while ( (filterfile = readdir(ld)) != NULL)
    {
    if ( strstr(filterfile->d_name, "txt") != NULL)
    {
    strcpy(filename,filterdir);
    strcat(filename,filterfile->d_name);
    if (fp = fopen(filename, "r") != NULL)
    {
    printf("%s\n", filename);
    sscanf(filterfile->d_name, "%d-%d.txt", &col, &row);
    mfilters[filtnum].col = col;
    mfilters[filtnum].row = row;
    mfilters[filtnum].filters= (float **)malloc(NWAVES * sizeof(float *));
    for (j = 0; j < NWAVES; j++)
    {
    if (mfilters[filtnum].filters[j] = (float *)malloc((12 + 3*j) * sizeof(float)) != NULL);
    fread(mfilters[filtnum].filters[j], 1, (12 + 3*j)*sizeof(float), fp);
    }
    }
    }
    filtnum++;
    }

    printf("%d\n", filtnum);
     
  2. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    First of all, you're leaking memory in this section. You are allocating memory for a single DIR type and then abandoning it without freeing (or using) it.
    Code:
    ld = (DIR *) calloc (1, sizeof(DIR));
    .
    .
    .
    ld = opendir(filterdir);
    As for the rest of the code, it would be helpful if you would explain what you are trying to accomplish instead of forcing us to guess and please use code tags!
     
  3. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    reply

    Thanks for responding.

    The code simply reads information stored in files into number of structures that hold the data.
    You're right that I don't free ld but I do use it in the readdir command. In any event, I don't have to free it until the end of the program.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Code:
    typedef struct matchedfilter{
    	int col;
    	int row;
    	float ** filters;
    	} matchedfilter;
    
    
    
    struct dirent  *filterfile;
    DIR *ld;
    matchedfilter *mfilters;
    int filtnum = 0;
    FILE * fp;
    char * filename;
    
    ld = (DIR *) calloc (1, sizeof(DIR));
    mfilters = (matchedfilter *)calloc(LENSLETS_SIZE*LENSLETS_SIZE, sizeof(matchedfilter));
    filename = (char*) calloc(25, sizeof(char));
    ld = opendir(filterdir);
    
    while (  (filterfile = readdir(ld)) != NULL)
    	{
    	if ( strstr(filterfile->d_name, "txt") != NULL)
    		{
    		strcpy(filename,filterdir);
    		strcat(filename,filterfile->d_name);
    		if (fp = fopen(filename, "r") != NULL)
    			{
    			printf("%s\n", filename);
    			sscanf(filterfile->d_name, "%d-%d.txt", &col, &row);
    			mfilters[filtnum].col = col;
    			mfilters[filtnum].row = row;
    			mfilters[filtnum].filters= (float **)malloc(NWAVES * sizeof(float *));
    			for (j  =  0; j < NWAVES; j++)
    				{
    				if (mfilters[filtnum].filters[j] = (float *)malloc((12 + 3*j) * sizeof(float)) != NULL);
    					fread(mfilters[filtnum].filters[j], 1, (12 + 3*j)*sizeof(float), fp);
    				}
    			}
    		}
    	filtnum++;
    	}
    
    printf("%d\n", filtnum);
    This was just posted with code tags for readability. I will look into what's actually going on now. And Guiyon was right about the opendir call. This is its signature:
    Code:
    DIR *opendir(const char *dirname);
    So you are assigning the value of the pointer that is returned to your pointer ld, so the original value of ld is not available, so you will never be able to free it. The root of the issue is, though, why do you need to allocate space for ld at all?

    Also, why alloc space for filename? Are you going to need it outside of the scope of this function? If not, why not just use a stack local variable so you don't have to remember to free it?

    -Lee
     
  5. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #5
    got it

    All the code is right, it is improved by the placement of some additional parentasees. Compiler warnings are sometimes more than warnings.
     

Share This Page