Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.os2.programmer.misc > #280

Passing string pointers in a custom control-data structure == FAIL

From "Alex Taylor" <mail.me@reply.to.address>
Newsgroups comp.os.os2.programmer.misc
Subject Passing string pointers in a custom control-data structure == FAIL
Date 2011-06-17 23:08 -0500
Organization Newscene Usenet News Service, http://www.newscene.com/
Message-ID <mdq090pMZSKk-pn2-EHH7HPrgtTe2@localhost> (permalink)

Show all headers | View raw


I have a weird problem with passing data to a custom window class on
WM_CREATE.  Basically, I have a control-data structure which gets 
passed to WinCreateWindow() that the control's window procedure uses
to initialize the control.

I've been doing this with various other controls and never had a problem.
What's different about this particular control is that the control-data
structure includes several string pointers.  (Normally I use static
arrays in data structures, but in this case I don't know even
approximately how long the strings are going to be, so I dynamically
allocate them instead.)

The main program allocates the strings (based on text loaded from
a resource DLL); the control's window procedure creates copies of
those strings during WM_CREATE processing so that the caller can 
safely free the originals.

Setting up the data structure is fine.  The problem is that, when it
gets passed to the control, for some reason all the string pointers 
are mangled (but the static fields are fine).  I've traced this as
much as I can in the IBM debugger (IPMD).  In the calling function,
the strings are fine.  As soon as WM_CREATE in the control's window
procedure receives the structure, the strings are reported as invalid
(presumably pointing off into never-never land).

Right now I'm using strdup() to allocate the strings in the structure,
but I've also tried using DosAllocMem() plus strcpy(), which makes no 
difference.

Clearly something's wrong with my memory management, but what?

Is using string pointers just a bad idea? 


The basic (stripped down) logic I'm using is reproduced below.


/**********************************************************
 * Relevant typedefs:
 */

  typedef struct _VI_CtlData {
      USHORT cb;         
      PSZ    pszFS,     
             pszSize;    
      // ...
  } VICTLDATA, *PVICTLDATA;


  typedef struct _VI_Private {
      VICTLDATA ctldata;      // public control-data structure
      ULONG     id;           // our control ID
      // ...
  } VIPRIVATE, *PVIPRIVATE;


/**********************************************************
 * In the application GUI setup:
 */

  // ...
  VICTLDATA infodata = {0};                    // control data
  CHAR      szRes1[ STRING_RES_MAXZ ] = {0},   // string buffers
            szRes2[ STRING_RES_MAXZ ] = {0};   // "

  // ...
  infodata.cb      = sizeof( VICTLDATA );
  infodata.pszFS   = strdup( szRes1 );
  infodata.pszSize = strdup( szRes2 );

  /*
   * At this point, infodata.pszFS and infodata.pszSize are correct
   * (as verified in the IBM debugger).
   */

  WinCreateWindow( hwnd, "VIClass", "", WS_VISIBLE | CS_SIZEREDRAW,
                   0, 0, 0, 0, hwnd, HWND_TOP, IDD_VI, &infodata, NULL );

  free( infodata.pszFS );
  free( infodata.pszSize );
  // ...


/**********************************************************
 * In the VI class's window procedure:
 */

  PVIPRIVATE pPrivate;    // pointer to private control data
  PVICTLDATA pPublic;     // pointer to public control data

  // ...
  case WM_CREATE:
      if ( !mp1 ) return (MRESULT) TRUE;
      pPublic = (PVICTLDATA) mp1;

      // initialize the private control data structure
      pPrivate = (PVIPRIVATE) calloc( 1, sizeof( VIPRIVATE ));
      if ( !pPrivate ) return (MRESULT) TRUE;

      pPrivate->ctldata.cb      = pPublic->cb;
      pPrivate->ctldata.pszFS   = strdup( pPublic->pszFS );
      pPrivate->ctldata.pszSize = strdup( pPublic->pszSize );
      pPrivate->id          = ((PCREATESTRUCT)mp2)->id;
      // ...

     /*
      * At this point, infodata.pszFS and infodata.pszSize are
      * reported by IBM debugger as "invalid string".
      */

      WinSetWindowPtr( hwnd, 0, pPrivate );
      return (MRESULT) FALSE;


  case WM_DESTROY:
      if (( pPrivate = WinQueryWindowPtr( hwnd, 0 )) != NULL ) {
          if ( pPrivate->ctldata.pszFS )
              free( pPrivate->ctldata.pszFS );
          if ( pPrivate->ctldata.pszSize )
              free( pPrivate->ctldata.pszSize );
          // ...
          free( pPrivate );
      }
      break;
  // ...


-- 
Alex Taylor
Fukushima, Japan
http://www.socis.ca/~ataylo00

Please take off hat when replying.

Back to comp.os.os2.programmer.misc | Previous | NextNext in thread | Find similar


Thread

Passing string pointers in a custom control-data structure == FAIL "Alex Taylor" <mail.me@reply.to.address> - 2011-06-17 23:08 -0500
  Re: Passing string pointers in a custom control-data structure == FAIL Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-06-18 09:23 +0200
    Re: Passing string pointers in a custom control-data structure == FAIL "Alex Taylor" <mail.me@reply.to.address> - 2011-06-18 06:05 -0500
  Re: Passing string pointers in a custom control-data structure == FAIL "A.D. Fundum" <what.ever@neverm.ind> - 2011-06-18 12:14 +0200
    Re: Passing string pointers in a custom control-data structure == FAIL "Alex Taylor" <mail.me@reply.to.address> - 2011-06-18 06:03 -0500
  Re: Passing string pointers in a custom control-data structure == FAIL "Alex Taylor" <mail.me@reply.to.address> - 2011-06-18 06:10 -0500
    Re: Passing string pointers in a custom control-data structure == FAIL Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-06-20 09:55 +0200
      Re: Passing string pointers in a custom control-data structure == FAIL "Alex Taylor" <mail.me@reply.to.address> - 2011-06-20 09:01 -0500
        Re: Passing string pointers in a custom control-data structure == FAIL "Steven Levine" <steve53@nomail.earthlink.net> - 2011-06-20 10:50 -0500
          Re: Passing string pointers in a custom control-data structure == FAIL Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-06-21 08:23 +0200
          Re: Passing string pointers in a custom control-data structure == FAIL "Alex Taylor" <mail.me@reply.to.address> - 2011-06-21 06:50 -0500
            Re: Passing string pointers in a custom control-data structure == FAIL "Steven Levine" <steve53@nomail.earthlink.net> - 2011-06-24 23:17 -0500
              Re: Passing string pointers in a custom control-data structure == FAIL "Alex Taylor" <mail.me@reply.to.address> - 2011-06-26 06:50 -0500
          The actual program "Alex Taylor" <mail.me@reply.to.address> - 2011-06-21 06:59 -0500
            Re: The actual program "Steven Levine" <steve53@nomail.earthlink.net> - 2011-06-24 14:42 -0500
              Re: The actual program "Alex Taylor" <mail.me@reply.to.address> - 2011-06-26 06:48 -0500
        Re: Passing string pointers in a custom control-data structure == FAIL Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-06-21 08:39 +0200
    Re: Passing string pointers in a custom control-data structure == FAIL Paul Ratcliffe <abuse@orac12.clara34.co56.uk78> - 2011-06-20 09:30 +0000
  Re: Passing string pointers in a custom control-data structure == FAIL Peter Flass <Peter_Flass@Yahoo.com> - 2011-06-18 08:08 -0400
  Re: Passing string pointers in a custom control-data structure == FAIL Paul Ratcliffe <abuse@orac12.clara34.co56.uk78> - 2011-06-18 18:07 +0000
    Re: Passing string pointers in a custom control-data structure == FAIL "Alex Taylor" <mail.me@reply.to.address> - 2011-06-20 09:01 -0500
      Re: Passing string pointers in a custom control-data structure == FAIL "Lars Erdmann" <lars.erdmann@arcor.de> - 2011-06-22 22:22 +0200
      Re: Passing string pointers in a custom control-data structure == FAIL Marty <net@comcast.martyamodeo> - 2011-06-24 10:45 -0700
        Re: Passing string pointers in a custom control-data structure == FAIL "Rich Walsh" <spamyourself@127.0.0.1> - 2011-06-25 03:04 +0000
          Re: Passing string pointers in a custom control-data structure == FAIL "Steven Levine" <steve53@nomail.earthlink.net> - 2011-06-24 22:43 -0500
            Re: Passing string pointers in a custom control-data structure == FAIL "Rich Walsh" <spamyourself@127.0.0.1> - 2011-06-26 07:11 +0000
  Re: Passing string pointers in a custom control-data structure == FAIL James Moe <jimoeDESPAM@sohnen-moe.com> - 2011-06-18 21:43 -0700

csiph-web