------------------------------------------------------------
                      [BUG/PRB.] VFP 9.0 FIX - GRID.OPTIMIZE PROPERTY
                                 January 2024
                 ------------------------------------------------------------
                                    CCB



1. BUG:

     In vfp9, we can set the Grid.Optimize property to true (.T.) for the Grid control uses Rushmore optimization,
     but sometimes VFP will crash.

     The bug was reported by Jurgen Wondzinski (wOOdy).


2. CAUSE:

     There are some BUGs in the following code.


3. RESOLUTION:

     We can write some code to fix the BUG.

     Fun51b751 :: ; proc near
             push ebp                                                        ;0x0051b751 :        55
             mov ebp , esp                                                   ;0x0051b752 :        8bec
             sub esp , 038h                                                  ;0x0051b754 :        83ec38
             push ebx                                                        ;0x0051b757 :        53
             push esi                                                        ;0x0051b758 :        56
             mov esi , ecx                                                   ;0x0051b759 :        8bf1
             mov  eax , dword ptr [esi]                                      ;0x0051b75b :        8b06
             mov  ecx , dword ptr [ eax + 12 ]                               ;0x0051b75d :        8b480c
             cmp ecx , edx                                                   ;0x0051b760 :        3bca
             push edi                                                        ;0x0051b762 :        57
             mov  dword ptr [ ebp - 8 ] , esi                                ;0x0051b763 :        8975f8
             jne Label51b774                                                 ;0x0051b766 :        750c
             mov  edi , dword ptr [ ebp + 8 ]                                ;0x0051b768 :        8b7d08
             and edi , 01h                                                   ;0x0051b76b :        83e701
             shl edi , 02h                                                   ;0x0051b76e :        c1e702
             or  dword ptr [ eax + 92 ] , edi                                ;0x0051b771 :        09785c

     Label51b774 ::
             mov eax ,  dword ptr [ Data9370f8 ]                             ;0x0051b774 :        a1f8709300
             mov  eax , dword ptr [eax]                                      ;0x0051b779 :        8b00
             cmp  dword ptr [ eax + 32 ] , edx                               ;0x0051b77b :        395020
             jne Label51b7e2                                                 ;0x0051b77e :        7562
             cmp ecx , edx                                                   ;0x0051b780 :        3bca
             jne Label51b7e2                                                 ;0x0051b782 :        755e
             mov eax ,  dword ptr [ Data9393ac ]                             ;0x0051b784 :        a1ac939300
             mov  dword ptr [ ebp - 12 ] , eax                               ;0x0051b789 :        8945f4
             mov eax , esi                                                   ;0x0051b78c :        8bc6
             call Fun519640                                                  ;0x0051b78e :        e8addeffff
             mov  ebx , dword ptr [esi]                                      ;0x0051b793 :        8b1e
             pushd 0Ah                                                       ;0x0051b795 :        6a0a
             lea esi ,  dword ptr [ ebx + 067Ch ]                            ;0x0051b797 :        8db37c060000
             pop ecx                                                         ;0x0051b79d :        59
             lea edi ,  dword ptr [ ebp - 52 ]                               ;0x0051b79e :        8d7dcc
             rep movsd                                                       ;0x0051b7a1 :        f3a5
             mov  dword ptr [ ebp + 8 ] , eax                                ;0x0051b7a3 :        894508
             mov  eax , dword ptr [ ebp - 36 ]                               ;0x0051b7a6 :        8b45dc
             xor edi , edi                                                   ;0x0051b7a9 :        33ff
             cmp eax , edi                                                   ;0x0051b7ab :        3bc7
             jne Label56b765                                                 ;0x0051b7ad :        0f85b2ff0400
             test  byte ptr [ ebx + 99 ] , 02h                               ;0x0051b7b3 :        f6436302
             jne Label56b773                                                 ;0x0051b7b7 :        0f85b6ff0400

     Label51b7bd ::


     ;
     ;                 -------------------------------------------------
     ;                      VFP 9.0 FIX - GRID.OPTIMIZE PROPERTY
     ;                                September 2015
     ;                 -------------------------------------------------
     ;                                     CCB
     ;
     ; If we set the Grid.Optimize property to true (.T.), sometimes VFP will crash.
     ;
     ; 2015/9/20, by ccb
     ;

             cmp dword ptr vfpa_sys9044_data,00h
             je Label51b7be
             mov  eax , dword ptr [ ebp - 36 ]
             test eax,eax
             je Label51b7bf
             push eax
             sub eax,08h
             push 08h
             push eax
             call vfpa_isbadreadptr
             test eax,eax
             pop eax
             jne Label51b7bf
             push eax
             sub eax,08h
             push dword ptr [eax+04h]
             push eax
             call vfpa_isbadreadptr
             test eax,eax
             pop eax
             jne Label51b7bf
             jmp Label51b7be
     Label51b7bf ::
             mov  dword ptr [ ebp - 36 ] , 00h


     Label51b7be ::
             mov  eax , dword ptr [ ebp - 36 ]                               ;0x0051b7bd :        8b45dc
             mov  esi , dword ptr [ ebp - 8 ]                                ;0x0051b7c0 :        8b75f8
             mov  dword ptr [ ebx + 068Ch ] , eax                            ;0x0051b7c3 :        89838c060000
             mov  eax , dword ptr [ ebp - 40 ]                               ;0x0051b7c9 :        8b45d8
             mov  dword ptr [ ebx + 0688h ] , eax                            ;0x0051b7cc :        898388060000
             mov  eax , dword ptr [ ebp + 8 ]                                ;0x0051b7d2 :        8b4508
             call Fun519661                                                  ;0x0051b7d5 :        e887deffff
             mov  eax , dword ptr [ ebp - 12 ]                               ;0x0051b7da :        8b45f4
             mov dword ptr [ Data9393ac ] ,  eax                             ;0x0051b7dd :        a3ac939300

     Label51b7e2 ::
             xor eax , eax                                                   ;0x0051b7e2 :        33c0
             inc eax                                                         ;0x0051b7e4 :        40
             lea esp ,  dword ptr [ ebp - 68 ]                               ;0x0051b7e5 :        8d65bc
             pop edi                                                         ;0x0051b7e8 :        5f
             pop esi                                                         ;0x0051b7e9 :        5e
             pop ebx                                                         ;0x0051b7ea :        5b
             leave                                                           ;0x0051b7eb :        c9
             ret 04h                                                         ;0x0051b7ec :        c20400

     Label56b765 ::


     ;
     ;                 -------------------------------------------------
     ;                      VFP 9.0 FIX - GRID.OPTIMIZE PROPERTY
     ;                                September 2015
     ;                 -------------------------------------------------
     ;                                     CCB
     ;
     ; If we set the Grid.Optimize property to true (.T.), sometimes VFP will crash.
     ;
     ; 2015/9/20, by ccb
     ;

             cmp dword ptr vfpa_sys9044_data,00h
             je Label56b766
             mov  eax , dword ptr [ ebp - 36 ]
             test eax,eax
             je Label56b76a
             push eax
             sub eax,08h
             push 08h
             push eax
             call vfpa_isbadreadptr
             test eax,eax
             pop eax
             jne Label56b76a
             push eax
             sub eax,08h
             push dword ptr [eax+04h]
             push eax
             call vfpa_isbadreadptr
             test eax,eax
             pop eax
             jne Label56b76a


     Label56b766 ::
             call Fun530453                                                  ;0x0056b765 :        e8e94cfcff

     Label56b76a ::
             mov  dword ptr [ ebp - 36 ] , edi                               ;0x0056b76a :        897ddc
             mov  dword ptr [ ebx + 068Ch ] , edi                            ;0x0056b76d :        89bb8c060000

     Label56b773 ::
             mov  esi , dword ptr [ ebx + 0120h ]                            ;0x0056b773 :        8bb320010000
             cmp esi , edi                                                   ;0x0056b779 :        3bf7
             je Label56b7c6                                                  ;0x0056b77b :        7449
             xor eax , eax                                                   ;0x0056b77d :        33c0
             call Fun42c118                                                  ;0x0056b77f :        e89409ecff
             mov eax , esp                                                   ;0x0056b784 :        8bc4
             sub  eax , dword ptr [ Data9370c4 ]                             ;0x0056b786 :        2b05c4709300
             cmp eax , esi                                                   ;0x0056b78c :        3bc6
             jle Label56b7a2                                                 ;0x0056b78e :        7e12
             mov eax , esi                                                   ;0x0056b790 :        8bc6
             add eax , 07h                                                   ;0x0056b792 :        83c007
             and eax , 0FFFFFFF8h                                            ;0x0056b795 :        83e0f8
             call Fun42c118                                                  ;0x0056b798 :        e87b09ecff
             mov  dword ptr [ ebp - 48 ] , esp                               ;0x0056b79d :        8965d0
             jmp Label56b7a5                                                 ;0x0056b7a0 :        eb03


4. APPLIES TO:

     VFP 9.0.0.2412
     VFP 9.0.0.3504 (SP1)
     VFP 9.0.0.4611 (SP2)
     VFP 9.0.0.5015 (SP2)
     VFP 9.0.0.5411 (SP2)
     VFP 9.0.0.5721 (SP2)
     VFP 9.0.0.5815 (SP2)
     VFP 9.0.0.6303 (SP2)
     VFP 9.0.0.6602 (SP2)
     VFP 9.0.0.7423 (SP2)

     The bug has been fixed in VFP Advanced.

     IMPORTANT NOTE:
     If we disable fixing the Grid.Optimize property, and set the Grid.Optimize property to true (.T.) for the Grid control,
     sometimes there is the memory access violation exception (exception code 0xC0000005).
     If we enable fixing the Grid.Optimize property, and set the Grid.Optimize property to true (.T.) for the Grid control,
     there is no the memory access violation exception (exception code 0xC0000005), but sometimes the index file will be corrupted.
     Recommend to set the Grid.Optimize property to false (.F.),
     there is no the memory access violation exception (exception code 0xC0000005), and the index file will not be corrupted.


5. REFERENCE WEBSITES:

     1, baiyujia.com:
     http://www.baiyujia.com
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix141.asp

     2, foxite.com:
     http://www.foxite.com/archives/grid-optimize-property-0000346194.htm

     3, microsoft.com:
     https://social.msdn.microsoft.com/Forums/de-DE/2b5034ee-4bb8-4267-9680-99306408ec64/c0000005absturz-im-grid-mit-optimizet


6. OTHER:

     For reference only, there is no guarantees.

     Any questions or suggestions, please send me an email at ccb2000@163.com.