C程序在调试模式下工作而不在发布模式下工作

C program working on debug mode and not working on release mode

我正在编写一个程序来处理存储在文件中的一些数据,当我 运行 它在代码块中处于调试模式时它 运行 非常完美,但每当我尝试制作它时它就会崩溃运行 不同(通过单击 exe 或尝试 运行 发布模式)我尝试了一切,我认为不应该有越界访问或未初始化的指针,这只是来自代码因为它在函数 readCSV 上停止,csv 文件包含 53 行并且计数行也能够在发布时正确地计算它们,printfs 只是了解它停止执行的位置我希望有一些错误我不能现货,在此先感谢您的帮助

typedef struct
{
 char* codice;
 char* nome;
 char* album;
 char* artista;
 int track;
 Durata durata;
 Genere genere;
}Brano;






   int countRowsCSV(const char* filename)
   {    FILE* file=fopen(filename,"r");
        char line[200];
        int i=0;
        if(file==NULL)
        return -1;
        while(!feof(file))
        {if(fgets(line,sizeof(line),file)!=NULL)
          i++;
        }
        fclose(file);
        return i;
   }



  char* getString(char* line)
 {
   printf("%s",line);
   char* a=NULL;
   a=(char*)malloc(sizeof(char)*strlen(line));
   strcpy(a,line);
   return a;
 }

 void readCSV(Brano* catalogo)
{
FILE* file=fopen("brani.csv","r");
char line[200]="";

int i;
if(file==NULL)
return ;

for (i=0;(!feof(file));i++)
{
    if(fgets(line,sizeof(line),file)!=NULL)
    {
        if(line[strlen(line)-1]!='[=10=]')//I remove the final \n
        line[strlen(line)-1]='[=10=]';
        printf("%s",line);
        catalogo[i].codice=NULL;
        catalogo[i].codice=getString(strtok(line,";"));
        printf("%s\n",catalogo[i].codice);
        catalogo[i].nome=NULL;
        catalogo[i].nome=getString(strtok(NULL,";"));
        printf("%s\n",catalogo[i].nome);
        catalogo[i].album=NULL;
        catalogo[i].album=getString(strtok(NULL,";")); //the program often crashes here with i=0
        printf("%s\n",catalogo[i].album);
        catalogo[i].artista=NULL;
        catalogo[i].artista=getString(strtok(NULL,";"));
        printf("%s\n",catalogo[i].artista);
        catalogo[i].track=atoi(strtok(NULL,";"));
        printf("%d\n",catalogo[i].track);
        catalogo[i].durata.minuti=atoi(strtok(NULL,";"));
        catalogo[i].durata.secondi=atoi(strtok(NULL,";"));
        catalogo[i].genere=stringToGenere(strtok(NULL,";"));
    }
}
fclose(file);

}

int main(void) {
int scelta=0,num_utenti=0,size_catalogo=countRows("brani.csv");
int size_publicP=0;

if(size_catalogo<0)
{ MessageBox(NULL,"Failed to open \"brani.csv\" ","ERROR MESSAGE",MB_ICONERROR|MB_ICONSTOP);
  return -1;
}
Brano* catalogo=NULL;
catalogo=(Brano*)malloc(sizeof(Brano)*size_catalogo);
Brano** pCatalogo=NULL;

Playlist** publicPlaylists=NULL;
Utente* loggedUser=NULL;
pCatalogo=(Brano**)malloc(sizeof(Brano*)*size_catalogo);
Utente* utenti=NULL;
readCSV(catalogo);

}

你的getstring()是错误的。它不考虑终止 NUL 字符。

  char* getString(char* line)
 {
   printf("%s",line);
   char* a=NULL;
   a=(char*)malloc(sizeof(char)*strlen(line));
   strcpy(a,line);
   return a;
 }

应该是

 char *getString(char *line)
 {
     printf("%s",line);
     char* a=malloc(1 + strlen(line));
     strcpy(a,line);
     return a;
 }

请注意 1 + strlen() 用于解释终止 NUL 字符。

另请注意,根据定义,sizeof(char) 始终为一,并且您不必在 C 中强制转换 void *

此外,see strdup()