Link to home
Start Free TrialLog in
Avatar of matlar
matlarFlag for Afghanistan

asked on

free() = Memory fault

When I try to free allocated memory I get "Memory fault".
The program look for a filename in an env-variable, and
opens the file via fopen.

Call to function :

FILE *sk_dd_fopen(char *namn_in, char *mode, char **filnamn_ut)

namn_in = name on env-variable (or real filename if variable don't  exist). Will be prefixed with "dd_". Only part up to first "." (if any) is used for env-name.
mode = mode as to fopen.
filnamn_ut = pointer to actual programname.
 
I'm running the program in UNIX on HP-UX.

This is the actual call to the function :
25  static FILE *l1lista = NULL;
   /* Globally declared within this file */

54   char filnamnÄ10Å="";
55   strncpy(filnamn, pnamn,7);
56   *(filnamn+7)='Ö0';
57   strcat(filnamn, "l1");
58   time(&tid);
62   if ((l1lista=sk_dd_fopen(filnamn,"w", NULL)) == NULL)
63       return (NULL);

The code of the funvcyion is provided below, and the line giving the problem is marked with comment
/* bailing out here */.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sk_konst.h>
#include <zz_medd_skriv.h>
#include "sk_gen_funk.h"


FILE *sk_dd_fopen(char *namn_in, char *mode, char **filnamn_ut)
{
    FILE *ret;
    char *dd_name = NULL;
    char *filename = NULL;
    char *namn_in_stripped = NULL;
    char *suffix = NULL;
    char *envname;
    char *anvmedd = "";


    /*  Plocka bort eventella suffix */
    namn_in_stripped = (char *)strdup((const char *)namn_in);
    if (strchr(namn_in_stripped, '.') != NULL)
       {
       suffix = strchr(namn_in_stripped, '.') + 1;
       *(strchr(namn_in_stripped, '.')) = '\0';
       }

    /* Reservera utrymme för namn på environmentvariabel  */
    if ( !(dd_name=malloc(strlen(namn_in_stripped + 4))) )
               /* fyra extra tecken för "dd_" och nullavslutning */
       {
       zz_medd_skriv(
                      ZZ_MEDD_MINDRE
             , __FILE__
               , __LINE__
               , ZZ_MEDD_APPL
               , "Fel vid allokering av minne"
               , anvmedd);
       return(NULL);
       }

    /* Hämta namn från variabel. Om saknas använd namn_in  */
    sprintf(dd_name,"dd_%s",namn_in_stripped);

    if ((envname=getenv(dd_name))== NULL )
       {
       envname = namn_in_stripped;
       }

    /* Reservera utrymme för fullständigt filnamn */
      filename = malloc(
         strlen(envname)
         + (suffix==NULL?0:strlen(suffix))
         + 2); /* 2 extra tecken för '.' och nullavslutning */

    /* Skapa fullständigt filnamn */
    if (suffix == NULL)
       strcpy(filename, envname);
    else
       if ( strlen(suffix) > 0)
          sprintf(filename, "%s.%s", envname, suffix);
       else
          strcpy(filename, envname);

    ret = ( fopen(filename, mode) );
    /* Follwing lines for debug only */
    printf("dd_name %d,%s\n",&dd_name,dd_name);
    printf("filename %d,%s\n",&filename,filename);
    printf("namn_in_stripped %d,%s\n"
           ,&namn_in_stripped,namn_in_stripped);
    printf("suffix %d,%s\n",&suffix,suffix);
    printf("envname %d,%s\n",&envname,envname);
    /* Returnera fullständigt filnamn  */

    if (filnamn_ut == NULL)
       free(filename);     /* bailing out here !! */
    else
       *filnamn_ut = filename;

    free (dd_name);
    free (namn_in_stripped);

    return (ret);
}


This is how it looks when I'm running a testprogram using
the funktion sk_dd_fopen.

dd_name 2063838844,dd_skib001l1
filename 2063838848,skib001l1
namn_in_stripped 2063838852,skib001l1
suffix 2063838856,
envname 2063838860,skib001l1
Memory fault(coredump)
/
Avatar of raky
raky

Well namn_in_striipped is never malloced I think
Edited text of question
Avatar of matlar

ASKER


Avatar of matlar

ASKER

Edited text of question
Avatar of matlar

ASKER

> When I try to free allocated memory I get "Memory fault".

In 9 of 10 cases it this caused because you access non allocated memory, e.g. access more memory than allocated.

It can also occur if you try to free not allocated memory.

It is not necessary in the function where the fail occour you access the unallocated memory, it can be in a different part of the program.

ASKER CERTIFIED SOLUTION
Avatar of tovergaard
tovergaard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial