www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/02/26/23:49:35

Date: Sun, 26 Feb 1995 18:43:24 -0500 (EST)
From: Kimberley Burchett <OKRA AT max DOT tiac DOT net>
Subject: free() clues
To: DJGPP Mailing List <djgpp AT sun DOT soe DOT clarkson DOT edu>

  Okay, I've done some more work and I've come up with some very strange 
results.  Here are two snippets from GDB sessions I've run.

  Here's the first one.

main (argc=2, argv=0x7ffffc9c) at showvga.cc:15
15        printf("mem left = %ld\n", _go32_dpmi_remaining_physical_memory());
(gdb) n
mem left = 831488
16        Image.~image();
(gdb) n
17        printf("mem left = %ld\n", _go32_dpmi_remaining_physical_memory());
(gdb) n
mem left = 823296
18      }
(gdb) q

  Here's the second one

main (argc=2, argv=0x7ffffc9c) at showvga.cc:15
15        printf("mem left = %ld\n", _go32_dpmi_remaining_physical_memory());
(gdb) n
mem left = 831488
16        Image.~image();
(gdb) s
_$_5image (this=0x7ffffc68, __in_chrg=2) at image.cc:22
22        if (channels == NULL)
(gdb) print this
$1 = (image *) 0x7ffffc68
(gdb) print this->channels
$2 = (JSAMPIMAGE) 0x15014
(gdb) until 200
main (argc=2, argv=0x7ffffc9c) at showvga.cc:17
17        printf("mem left = %ld\n", _go32_dpmi_remaining_physical_memory());
(gdb) n
mem left = 786432
18      }
(gdb) q


  What this shows is that if I trace into my destructor function with GDB
_and_ I have it print out a few pointers, it will free my memory. 
Otherwise, it only frees a little of it.  Also, if I just print out "this"
and not "this->channels", the second call to remaining_memory() returns
794624 - different from both of the above!

  Now it seems to me that this kind of behaviour indicates a dangling
pointer somewhere that GDB is overwriting somehow.  But given how long and
how thoroughly I've worked on this, I find that really hard to believe. 
Besides - after putting the class through the following test program
without it crashing, I find dangling pointers _really_ hard to believe. 

#include "image.h"
#include "video.h"
#include "files.h"

int main(int argc, char *argv[]) {

  image Image = ReadJPEG(argv[1]);

  // resize the image
  Image.SetSize(Image.Width() + 100, Image.Height() + 100);
  Image.SetSize(Image.Width() - 100, Image.Height() - 100);

  // change the color space
  Image.SetColorSpace(GreyScale);
  Image.SetColorSpace(RGB);

  // fiddle with the channels
  Image.AddChannels(2);
  Image.RemoveChannel(5);
  Image.RemoveChannel(4);
  Image.AddChannels(2);
  Image.RemoveChannel(4);
  Image.RemoveChannel(5);

  image ImagePiece = Image.Cut(rect(100,100,200,200));

  // draw our result - should be a greyscale version of original
  StartVideo(320,240,16);           // 320x240x64k colors
  ScreenBlock(point(0,0), ImagePiece);

  // give us time to see the result
  while(!kbhit());
  StartVideo(80,50);
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019