This has nothing to do with Android, per se.

Calling free(x) when x is NULL is not harmful.
Calling free(x) when x came from malloc() is not harmful.
Calling free(x) twice with the same value of x will surely SIGSEGV.
Calling free(x) with a bad value will likely SIGSEGV.

Note case 3:  if you call free(x), follow it immediately with x=NULL,
and you will probably not crash anymore.  Matching your malloc()/
free()
calls carefully and logically tends to fix such problems.




On Mar 22, 4:26 am, Dewr <wind8...@gmail.com> wrote:
> I have tested it on Galaxy S and it worked well on Galaxy S (2.2). I have
> not tried it on another Nexus One in order to know if my Nexus One is
> faulty.
>
> and it seems like that AssetFileDescriptor is not the source of the problem,
> because the problem is still ocurring while I didn't access asset files
> directly but accessing copied ones.
>
>
>
> On Fri, Feb 25, 2011 at 1:10 PM, Dewr <wind8...@gmail.com> wrote:
> > it often causes SIGSEGV on third for-loop. but just a moment ago SIGSEGV on
> > fourth loop.
>
> >>     for ( i = 0 ; i < 4 ; i++ ) {
> >>         sprintf (temp, "%s%s", header[i], index[fileno[i]]);
> >>         *strBuf = getTextByIndex(&filebuf, temp);* *//malloc() in this
> >> function.*
> >>         if (strBuf == NULL)
> >>             buf_output[i] = NULL;
> >>         else
>
> >>         {
> >>             buf_output[i] = (*gEnv)->NewStringUTF(gEnv, strBuf);
> >> *            free(strBuf);*
> >>         }
> >>     }
>
> > On Thu, Feb 24, 2011 at 1:15 PM, Dewr <wind8...@gmail.com> wrote:
>
> >> Hello, I am porting a C program.
>
> >> The problem I've met is SIGSEGV on free(). I can't see what's the root
> >> cause of it.
> >> it doesn't occur sometimes, but very often.
>
> >> I am testing it on NexusOne 2.2.1
> >> I am using Android NDK r5b and Android SDK and Eclipse ADT and Cygwin.
>
> >> I am using *android.content.res.AssetFileDescriptor* to read assets in C
> >> modules.
>
> >> Here is the messages in ndk-gdb when the problem appears.
>
> >>> (gdb) c
> >>> Continuing.
>
> >>> Breakpoint 2, Java_kr_co_pkbio_Unse_DangSaJuShinSal (env=0xaa50,
> >>>     obj=0x4495b970)
> >>>     at
> >>> C:/DEWR/Product/Software-Engineering/Eclipse-Workspace/Unse/jni/unse.c:1
> >>> 83
> >>> *1083                    free(strBuf);*
> >>> (gdb) next
>
> >>> *Program received signal SIGSEGV, Segmentation fault.*
> >>> *0xafd11c80 in __libc_android_abort ()*
> >>>    from
> >>> C:/DEWR/Product/Software-Engineering/Eclipse-Workspace/Unse/obj/local/a
> >>> meabi/libc.so
> >>> (gdb) bt
> >>> #0  0xafd11c80 in __libc_android_abort ()
> >>>    from
> >>> C:/DEWR/Product/Software-Engineering/Eclipse-Workspace/Unse/obj/local/a
> >>> meabi/libc.so
> >>> #1  0xbec233bc in ?? ()
> >>> Cannot access memory at address 0xc
> >>> (gdb) quit
>
> >> Here is the Java source code...
>
> >>>     public static FileInfoForNativeCode openAssets(String fname) {
> >>>         if (Constants.VERBOSE_LOGS)
> >>>             Log.d(TAG, "openAssets("+fname+")");
>
> >>>         *AssetFileDescriptor myDescriptor = null;*
> >>>         try {
> >>>             myDescriptor = context.getAssets().openFd(fname+".jet");
> >>>         } catch (IOException e) {
> >>>             e.printStackTrace();
> >>>             return null;
> >>>         }
> >>>         FileDescriptor fd = myDescriptor.getFileDescriptor();
> >>>         long off = myDescriptor.getStartOffset();
> >>>         long len = myDescriptor.getLength();
>
> >>>         if (Constants.VERBOSE_LOGS)
> >>>             Log.d(TAG, "fd:"+fd+" off:"+off+" len:"+len);
>
> >>>         return new FileInfoForNativeCode(off, len, fd);
> >>>     }
>
> >> Here is the C source code...
>
> >>> char* getTextByIndex (TextFileBufType *filebuf, char *index) {
> >>>     #define _INDEX_PREFIX_    '@'
> >>>     int        i, j, lenBuf;
> >>>     char    *result;
> >>>     char    indexPrefix = _INDEX_PREFIX_;
> >>>     int        lenIndexPrefix = utf8len( &indexPrefix );
> >>>     int        lenIndex = strlen(index);
>
> >>>     for ( i = 0 ; i < filebuf->total ; i++ ) {
> >>>         *//__android_log_print(ANDROID_LOG_DEBUG,"TAG", "JNI : %d ->
> >>> %s", i, filebuf->text[i]);*
>
> >>>         if ( memcmp (filebuf->text[i], &indexPrefix, lenIndexPrefix) != 0
> >>> )
> >>>             continue;
>
> >>>         if ( memcmp (filebuf->text[i]+lenIndexPrefix, index, lenIndex) !=
> >>> 0 )
> >>>             continue;
>
> >>>         lenBuf = 0;
> >>>         lenBuf += strlen(filebuf->text[i]);
> >>>         lenBuf++;
> >>>         for ( j = i+1 ; j < filebuf->total ; j++ ) {
> >>>             if ( memcmp (filebuf->text[j], &indexPrefix, lenIndexPrefix)
> >>> == 0 )
> >>>                 break;
>
> >>>             lenBuf += strlen(filebuf->text[j]);
> >>>             lenBuf++;
> >>>         }
>
> >>>         *result = malloc(lenBuf);*
> >>>         result[0] = 0;
> >>>         strcat(result, filebuf->text[i]);
> >>>         strcat(result, "\n");
> >>>         for ( j = i+1 ; j < filebuf->total ; j++ ) {
> >>>             if ( memcmp (filebuf->text[j], &indexPrefix, lenIndexPrefix)
> >>> == 0 )
> >>>                 break;
>
> >>>             strcat(result, filebuf->text[j]);
> >>>             strcat(result, "\n");
> >>>         }
>
> >>>         *//__android_log_print(ANDROID_LOG_DEBUG,"TAG", "JNI : %d!!! ->
> >>> %s", i, filebuf->text[i]);*
> >>>         *return result;*
> >>>     }
>
> >>>     return NULL;
>
> >>>     #undef _INDEX_PREFIX_
> >>> }
>
> >>> inline void readyFileInFile (FileInFile *fif, char *path)
> >>> {
> >>>     jstring jstrFpath;
> >>>     jobject finfo;
> >>>     jobject descriptor;
>
> >>>     jstrFpath = (*gEnv)->NewStringUTF(gEnv, path);
> >>>     finfo = (*gEnv)->CallStaticObjectMethod(gEnv, clsUtility,
> >>> midOpenAssets, jstrFpath);
> >>>     fif->offset = (*gEnv)->GetLongField(gEnv, finfo, fidOffset);
> >>>     fif->length = (*gEnv)->GetLongField(gEnv, finfo, fidLength);
> >>>     descriptor = (*gEnv)->GetObjectField(gEnv, finfo, fidDescriptor);
> >>>     fif->fd = (*gEnv)->GetIntField(gEnv, descriptor,
> >>> fidDescriptorFileDescriptor);
> >>> }
>
> >>> jobjectArray Java_kr_co_pkbio_Unse_DangSaJuShinSal (JNIEnv *env, jobject
> >>> obj) {
> >>> *//    char   *fname = "DangSaJu_ShinSal";*
> >>>     int     i, fileno[4], ret_code, type;
> >>>     char    temp[256];
> >>>     char   *header[4] = { "년", "월", "일", "시" };
> >>>     char    table1[12] = { 1, 0, 3, 2, 1, 0, 3, 2, 1, 0, 3, 2 };
> >>>     char    table2[4]  = {10, 7, 4, 1};
>
> >>>     char   *index[12] = {
> >>>         "겁살",   "재살",   "천살",   "지살",   "연살",   "월살",
> >>>         "망신살", "장성살", "반안살", "역마살", "육해살", "화개살"
> >>>     };
>
> >>>     int        r_option;
> >>>     int        numBytesRead;
> >>>     char    *strBuf;
> >>>     TextFileBufType    filebuf;
> >>>     jstring buf_output[4];
> >>>     jobjectArray output;
> >>>     FileInFile fif;
>
> >>>     gEnv = env;
>
> >>>     */* 년월일시에 대한 겁살을 계산한다 */*
> >>>     type = table1[BaseInfo.saju.day % 12];
> >>>     fileno[0] = (table2[type] + (BaseInfo.saju.year  % 12)) % 12;
> >>>     fileno[1] = (table2[type] + (BaseInfo.saju.month % 12)) % 12;
> >>>     fileno[2] = (table2[type] + (BaseInfo.saju.day   % 12)) % 12;
> >>>     fileno[3] = (table2[type] + (BaseInfo.saju.hour  % 12)) % 12;
> >>>     */*WriteMsg (" - 기본위치 : 생일지지(%s) --> type:%d --> 자(%s)에 대한 살:%s\n",
> >>>         JeeJeeStr[BaseInfo.saju.day % 12], type, JeeJeeStr[0],
> >>> index[table2[type]]);
> >>>     WriteMsg (" - 년살     : 생년지지(%s) --> 년%s\n",
> >>> JeeJeeStr[BaseInfo.saju.year % 12],
> >>>         index[fileno[0]]);
> >>>     WriteMsg (" - 월살     : 생월지지(%s) --> 월%s\n",
> >>> JeeJeeStr[BaseInfo.saju.month % 12],
> >>>         index[fileno[1]]);
> >>>     WriteMsg (" - 일살     : 생일지지(%s) --> 일%s\n",
> >>> JeeJeeStr[BaseInfo.saju.day % 12],
> >>>         index[fileno[2]]);
> >>>     WriteMsg (" - 시살     : 생시지지(%s) --> 시%s\n",
> >>> JeeJeeStr[BaseInfo.saju.hour % 12],
> >>>         index[fileno[3]]);*/*
>
> >>>     readyFileInFile(&fif, "data/DangSaJu5.dat");
>
> >>>     r_option = ((int)'#' << 8) & 0xFF00;   /* Comment char setting */
> >>>     r_option |= (RT_OPT_RMCMT | RT_OPT_RMCRLF | RT_OPT_LTRIM |
> >>> RT_OPT_RTRIM | RT_OPT_SPACES);
>
> >>>     if ( ReadTextFileToBufA (&filebuf, &fif, r_option) < 0 ) {
> >>>         __android_log_print(ANDROID_LOG_ERROR,"TAG", "JNI : 서버측에서 치명적인
> >>> 오류가 발생하여 더이상 진행할 수 없습니다.");
> >>>         return NULL;
> >>>     }
>
> >>>     for ( i = 0 ; i < 4 ; i++ ) {
> >>>         sprintf (temp, "%s%s", header[i], index[fileno[i]]);
> >>>         *strBuf = getTextByIndex(&filebuf, temp);*
> >>>         *//__android_log_print(ANDROID_LOG_DEBUG,"TAG", "JNI : %s ->
> >>> %s", temp, strBuf );*
> >>>         buf_output[i] = (*gEnv)->NewStringUTF(gEnv, strBuf);
> >>> *        free(strBuf); //line number : 1083 ***It doesn't cause SIGSEGV
> >>> on first for-loop-round, but on third for-round.****
> >>>         *//PrintTextFile3 ("../data/DangSaJu5.dat", temp, VIEW_T_R_STR,
> >>>         //    "12신살로 알아보는 인생의 길흉화복 : %s", temp);
> >>>         /*if (i < 3) {
> >>>             sprintf (temp, "\n 계속해서 %s%s에 대한 풀이를 보시겠습니까? (Y/n)",
> >>>                 header[i+1], index[fileno[i+1]]);
> >>>             ret_code = GetYesNo (temp, GETYN_YES);
> >>>             if (ret_code == GETYN_NO)  return;
> >>>         }*/*
> >>>     }
>
> >>>     FreeTextFileBuf (&filebuf);
> >>>     *//printf ("\n [Enter]키를 누르세요.");  fflush(stdout);
> >>>     //PauseUntil (300, 0);*
> >>>     *#if defined (_DO_NOT_COMPILE_)
> >>>     {
> >>>         int j;
> >>>         for (i=0 ; i<4 ; i++) {
> >>>             for (j=0 ; j<12 ; j++) {
> >>>                 sprintf (temp, "%s%s", header[i], index[j]);
> >>>                ** //PrintTextFile3 ("../data/DangSaJu5.dat", temp,
> >>> VIEW_T_R_STR,
> >>>                 //    "당사주 / 인생 길흉화복 datafile test : %s", temp);
> >>>             }
> >>>         }
> >>>     }
> >>>     #endif*
>
> >>>     output = (*gEnv)->NewObjectArray(gEnv, 4, clsString, NULL);
> >>>     (*gEnv)->SetObjectArrayElement(gEnv, output, 0, buf_output[0]);
> >>>     (*gEnv)->SetObjectArrayElement(gEnv, output, 1, buf_output[1]);
> >>>     (*gEnv)->SetObjectArrayElement(gEnv, output, 2, buf_output[2]);
> >>>     (*gEnv)->SetObjectArrayElement(gEnv, output, 3, buf_output[3]);
>
> >>>     return output;
> >>> }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to