ASSERT fail Solution: Share with EveryOne who read it 
Author Message
 ASSERT fail Solution: Share with EveryOne who read it

   Some Days ago, I post a question, the title is " What the ASSERT
fail tell me?". The problem is:
   1. When running, the global pointer pDoc[i] changes to NULL, thus
the program stoped.
   2. After exit program, It says " Access Violation ...", and Debug
infos says Memory Leaks.

   Because my program is big, It takes me more than 10 days to get the
problem. I was very hurry. It's a really problem.
   While finding my problem, many friends helped me. especially there
are: Tomas Restrepo, Dirk Bohnemeyer, Dan Evens. I've not seen them,
but they kindly helped me. Thanks to them very much. Thanks to
NewsGroup and Internet too.
   My classmate Tony Tan helped me too.
   So I post this solution, hope has some help to others.

   Error 1: strset error
      in Link.H
        struct lineQueue {
           struct LINE line;
           struct lineQueue *next;
        };

        Class CLink{
                struct lineQueue *lineHead;
                char   buf[100];
                ...
        }      

     in Link.cpp
        CLink::CLink(){
                //this line makes ACCESS Violation error
                strset( disp_buf, '0' );
                // change it to following line, OK
                for( i=0; i<100; i++ )
                        buf[i] = 0;
        }

    Error 2: memory free error
        // the wrong way: leads memory leak
        CLink::~CLink(){
                if( lineHead !=NULL ){
                        while( lineHead->next !=NULL )
                                free( lineHead->next);
                        free ( lineHead );              
                }
        }

        // the right way
        CLink::~CLink(){
                while(lineHead != NULL) {
                     temp = lineHead->next;
                     free(lineHead);
                     lineHead = temp;
                }
        }

    Error 3: function return error, lead pDoc[i]=NULL.
        // wrong case
        struct LINE CLink::popLineQueue(lineQueue *head)
        {
           LINE line;

           if(lineHead != NULL){
             line = lineHead->line;
             temp = lineHead->next;
             free(lineHead);
             lineHead = temp;
             return line; // It's wrong, because return local memory is                        
unbelivable
          }
        }

        // right way:
        void CLink::popLineQueue(lineQueue *head)
        {
           if(lineHead != NULL){
             m_gLine = lineHead->line; // here m_gLine is member of CLink.
             temp = lineHead->next;
             free(lineHead);
             lineHead = temp;
          }
        }

  regards
  donglz



Sun, 01 Apr 2001 03:00:00 GMT  
 ASSERT fail Solution: Share with EveryOne who read it

Quote:

>   Error 1: strset error
>      in Link.H
> Class CLink{
> struct lineQueue *lineHead;
> char   buf[100];
>     in Link.cpp
>        CLink::CLink(){
> //this line makes ACCESS Violation error
> strset( disp_buf, '0' );
> // change it to following line, OK
>  for( i=0; i<100; i++ )
> buf[i] = 0;
> }

    While this will work, there is an easier (and better) way.
        memset(buf,'\0', sizeof(buf));

    Your original version failed for several reasons (I'm assuming disp_buf
pointed to buf).  strset() sets the contents of a "string" to the specified
character.   In C, a string is a series of characters, followed by a byte of
0x00.  Here, there was no previous assignment to buff, so that there was no
guarantee that there was a 0x00 byte with the first 100 bytes of buf.  If
there wasn't, strset would just keep going, filling every character in
memory until it found a 0x00 byte.     The memset is given a definite size
to fill, so it knows where to stop.  sizeof() is better that hardcoding the
len, because if you want to change it later, you only have to change it in
one place not two.   Also note that in the original version, you were
filling with a '0' character ( ASCII code 0x30), while in the revised
version, you were using a NUL character (ASCII code 0x00).

Quote:

>    Error 3: function return error, lead pDoc[i]=NULL.
>        // wrong case
> struct LINE CLink::popLineQueue(lineQueue *head)
> {
>    LINE line;

>    if(lineHead != NULL){
>      line = lineHead->line;
>      temp = lineHead->next;
>      free(lineHead);
>      lineHead = temp;
>      return line; // It's wrong, because return local memory is
>unbelivable
>   }
> }

    Actually, there's no problem here (well, at least, not the one you
cited).  You are NOT returning local memory. You are return a COPY of the
local variable.   The completely correct ('though it might be ineffecient).
However, it should be noted that you only return a value when lineHead !=
NULL.  Which means that the return value when lineHead==NULL is undefined
(which means trouble).

Quote:
> // right way:
> void CLink::popLineQueue(lineQueue *head)
> {
>    if(lineHead != NULL){
>      m_gLine = lineHead->line; // here m_gLine is member of CLink.
>      temp = lineHead->next;
>      free(lineHead);
>      lineHead = temp;
>   }
> }

    Also, you should note that in neither of these sample, do you use the
parameter.

--
Truth,
   James [MVP]
http://www.NJTheater.Com       -and-
http://www.NJTheater.Com/JamesCurran

begin 666 James M. Curran.vcf


M4DLZ.SLQ."!*;VAN(%-T+"!3=6ET92 R0CM";&]O;69I96QD.TY*.S W,# S
M+34Q-#D-"DQ!0D5,.U=/4DL[14Y#3T1)3D<]455/5$5$+5!224Y404),13HQ
M."!*;VAN(%-T+"!3=6ET92 R0CTP1#TP04)L;V]M9FEE;&0L($Y*(# W,# S
M+34Q-#D-"E523#IH='1P.B\O=W=W+DY*5&AE871E<BYC;VTO2F%M97-#=7)R
M86X-"E523#IH='1P.B\O=W=W+DY*5&AE871E<BYC;VT-"D5-04E,.U!2148[
M24Y415).150Z2F%M97-#=7)R86Y 0V]M<'5397)V92YC;VT-"E)%5CHQ.3DX

`
end



Mon, 02 Apr 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. EVERYONE, PLEASE READ THIS!!

2. Loading solutions from a shared environment

3. how to share the codes in two projects of one solution in visual studio .net

4. Assert Failed : pClientSite == 0 || m_spClientSite == 0 with activeX

5. Connectable ATL issue : CAxDialogImpl assert failed

6. Debug Assertion Failed - problem/solution

7. ASSERT failed after Loading a DLL

8. CTreeCtrl.DeleteAllItems fails ASSERT

9. Problem about Dialog with ASSERT(ContinueModal()) failed

10. What the ASSERT fail tell me?

11. Assert failed due to UpdateData too fast?

12. Thread Debug Assert Failed

 

 
Powered by phpBB® Forum Software