[racket] Some problem with hash-update!

From: Evgeny Odegov (oev-racket at sibmail.com)
Date: Sat Sep 13 10:28:10 EDT 2014

Hi, all!

I have a problem with the code below:

(define result (make-hash))
(define (callback dwTextID lpszInfoText lpvUser)
  (hash-update! result
                lpszInfoText
                (lambda (id-lst)
                  (define v (list dwTextID))
                  (if id-lst (append id-lst v) v))
                #f)
  #t)

(The `callback' is passed to some enumeration function via FFI. Its type
is defined as:

(define _TXT_ENUM_INFOTEXTS_PROC
  (_cprocedure (list _DWORD _string/locale _pointer)
               _bool
               #:abi 'sysv)).


It seems like problem not with FFI, but I could give more details if needed.)

Mostly, parameter `lpszInfoText' is unique, so length of `is-lst' is
rarely more than one.
But, when there are lot of entries with empty `lpszInfoText', the list
becomes big.
At some moment something goes wrong, and my program fails with error:

append: contract violation
  expected: list?
  given: (list* 80 579 580 581 582 583 584 585 586 587 588 589 590 591 592
593 594 595 596 597 598 599 600 602 603 604 605 606 607 608 609 610 612
613 614 615 616 617 618 619 621 622 623 624 625 626 627 628 629 631 632
633 634 635 636 637 638 640 641 642 6...


I add a code that prints the wrong list before `append'. Result differs
from run to run, but always belongs to one of two categories.

Examples:
* http://pastebin.com/bnNYY8EL
* http://pastebin.com/x7nDdnWn


If I change the code to below, it works without problems:

(define result (make-hash))
(define big-list (list))
(define (callback dwTextID lpszInfoText lpvUser)
  (if (equal? lpszInfoText "")
      (set! big-list (cons dwTextID big-list))
      (hash-update! result
                    lpszInfoText
                    (lambda (id-lst)
                      (define v (list dwTextID))
                      (if id-lst (append id-lst v) v))
                    #f))
  #t)


Racket version: 6.1 release, x86, 32-bit
OS: Windows XP Prof 32 bit


Posted on the users mailing list.