delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/07/16/10:45:06

From: Peter Claessens <peter DOT claessens AT psy DOT kuleuven DOT ac DOT be>
Newsgroups: comp.os.msdos.djgpp
Subject: malloc/free blues
Date: Wed, 16 Jul 2003 16:41:56 +0200
Organization: KULeuvenNet
Lines: 200
Message-ID: <3F156434.3000508@psy.kuleuven.ac.be>
NNTP-Posting-Host: seven.kulnet.kuleuven.ac.be
Mime-Version: 1.0
X-Trace: naxos.belnet.be 1058366516 29864 134.58.127.12 (16 Jul 2003 14:41:56 GMT)
X-Complaints-To: abuse AT belnet DOT be
NNTP-Posting-Date: Wed, 16 Jul 2003 14:41:56 +0000 (UTC)
User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01
X-Accept-Language: nl-BE,en
Cache-Post-Path: seven.kulnet.kuleuven.ac.be!unknown AT p101pw099 DOT psy DOT kuleuven DOT ac DOT be
X-Cache: nntpcache 2.4.0b5 (see http://www.nntpcache.org/)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hey people,

I have been chasing a bug in my program since a long time now. I am 
running out of both time and ideas.
This is the problem:
When I run the program, or at least the functions that I need, I 
encounter different types of runtime errors - either a segmentation 
fault, a page fault in both reading and writing, a freeze, and even a 
spontaneous reboot. And which is worse, the crashes occur at different 
points in the code. When I add statements meant for debugging, or run 
the program at different moments, the crash point often shifts.
 From the types of errors, and form output of bfdsymify and gdb(5.3),  I 
learned that it had something to do with memory allocation or 
deallocation. Not surprising in a sense, since I have to allocate large 
chuncks of memory to create bitmaps (I am using allegro 4.0.3 - 
debugging version). Even using yamd (is it ok in combination with 
gcc3.2.3?) I can't figure out where I am making a mistake - also under 
yamd it crashes at different points. Btw, as to the allegro functions, 
you would expect them to return NULL when they are unable to allocate 
enough memory for a bitmap, for example, but they don't. Also here I 
simply get runtime errors in malloc/free functions, within the allegro 
library.
The crashes occur in the environment that I usually run the program in 
(dos box in win98), but also in MS-DOS 6.22 with CWSDPMI, that's why I 
think it's not one of the win9x-related memory issues described in the FAQ.
The program code is way too big to paste here, but at the bottom of this 
mail you will find a stripped-down version of the function that the 
program crashes in, without the actual drawing functions. I left in some 
crucial assignments. This function is embedded in a whole set of class 
declarations, so I am afraid that I can't send a readily compilable 
minimal code.
Of course if anyone would feel like taking a look, I wouldn't mind 
sending the entire compilable source code of the program, together with 
the configuration files it needs to run, or an executable with debugging 
symbols.
Maybe I am looking over something very simple. I wouldn't be surprised, 
I am autodidact in programming, and I miss some of the basis. Can 
someone tell me what I'm doing wrong, or what would be the next option 
in the debugging process?

Thanks,
Peter.

The philosophy behind the code is the following. This program is meant 
to run, after an initial screen, sequences of image frames with brief 
exposure durations, followed by a response screen. The contents of the 
frames are a type of geometric objects (this is for a visual perception 
experiment).
hierarchically: trial->sequence->frame (several, ordered)
a frame 'contains' a dot lattice (or another type of geometric object)
Typical values for the radius is 380, for dot radius something like 10, 
and a screen resolution of 1024*768 pixels.

int sequence_trial2(trial* trialp) {
/* INITIALIZING, PREPARING SEQUENCE */
  sequence* seqP=trialp->seqp;
  vector<frameinfo*>* frameinfovecptr;
  frameinfovecptr=seqP->get_frameinfovecptr();
  dotlattice* dltest=new dotlattice;
  string latticeid=typeid(*dltest).name();
  delete dltest;
  double radius_, center_x, center_y;
  if (initflag==false) {
    frameinfo* frameinfoP=(*frameinfovecptr)[0];
    void* frameP=frameinfoP->get_frameP();   
    if (frameinfoP->get_frametype()==latticeid) {
      dotlattice* dotobjptr=reinterpret_cast<dotlattice*>(frameP);
      radius_=dotobjptr->getbound().boundparam[0];
      center_x=dotobjptr->getcenter().x;
      center_y=dotobjptr->getcenter().y;   
    }
    trials_init(round(radius_),round(center_x),round(center_y));
    if (initflag==false) {
      (*errlogp) << "Graphics mode not initialized." << endl;
      return 1;
    }
    initflag=2;
  }
  // sometimes problems in next line
  if (fixpage==NULL) 
fixpage=create_bitmap(round(2*radius+1),round(2*radius+1));
  if (fixpage==NULL) {  cerr << "Problem acquring bitmap memory.";  
cin.get();  return 1;  }
  double orienta=DBL_MAX;
  double boa=1;
  double gamma=M_PI/2.0;
  int mirrorflag=0;
  for (int i=frameinfovecptr->size()-1; i>=0;i--) {
    frameinfo* frameinfoP=(*frameinfovecptr)[i];
    void* frameP=frameinfoP->get_frameP();   
    if (frameinfoP->get_frametype()==latticeid) {
      dotlattice* dotobjptr=reinterpret_cast<dotlattice*>(frameP);
      orienta=dotobjptr->getorient();
      boa=dotobjptr->getbea();
      gamma=dotobjptr->getgamma();
      mirrorflag=dotobjptr->getmirrorflag();
      break;
    }
  }
  int dotdiam=round(trialp->fixdotradius*2.0+1.0);
  BITMAP* dot=create_bitmap(dotdiam,dotdiam);
  if (dot==NULL) {  cerr << "Problem creating bitmap 'dot'." << endl;  
return 0;  }
  dotcoint p01,p02;
  p01.x=p01.y=round(trialp->fixdotradius);
  double d;
  for (p02.x=0; p02.x<dotdiam ; p02.x++) {
    for (p02.y=0; p02.y<dotdiam; p02.y++) {
      d=distancexy(p01,p02);
      if (!(d>(trialp->fixdotradius+0.0))) putpixel(dot,p02.x,p02.y,red);
    }
  }
// sometimes problems here
  destroy_bitmap(fixpage);
// sometimes problems here
  if (resp_bmp==NULL) resp_bmp=create_bitmap(xmax,ymax);
  if (resp_bmp==NULL) {  set_gfx_mode(GFX_TEXT,0,0,0,0);  cerr << 
"Insufficient memory to create image."; cin.get();   return 1;  }
  vector<dotco>* respvecptr=prepare_response(resp_bmp,5,orienta, boa, 
gamma, mirrorflag);
  BITMAP* page;
/* CREATE THE BITMAPS - problems!*/
  for (int i=0; i<frameinfovecptr->size(); i++) {
    frameinfo* frameinfoP=(*frameinfovecptr)[i];
    if (frameinfoP->get_frametype()==latticeid) {
      void* frameP=frameinfoP->get_frameP();
      dotobject* dotobjptr=reinterpret_cast<dotlattice*>(frameP);
      frameinfoP->set_lifetime(sequence_life);
      if(dotobjptr->dotfill()!=0) { cerr << "Problem with field 
object."; return 1; }
      page=NULL;
      screenvec.clear();  
      dotcovector* dotarrptr=dotobjptr->getdotarrP();
      int radius=round(dotobjptr->getbound().boundparam[0]);
      int dotradius=round(dotobjptr->getdotradius());
      dotcoint center, xy;
      center.x=round(dotobjptr->getcenter().x);
      center.y=ymax-round(dotobjptr->getcenter().y);
      for (dotcovector::iterator 
i=(*dotarrptr).begin();i!=(*dotarrptr).end();i++) {
        xy.x=round((*i).x);
        xy.y=round(ymax-(*i).y);
        screenvec.push_back(xy);
      }
      if (page==NULL) page=create_bitmap(2*radius+1,2*radius+1);
      if (page==NULL) { set_gfx_mode(GFX_TEXT,0,0,0,0); cerr << 
"Insufficient memory to create image."; cin.get(); return 1;   }
      int xoffset=round((xmax-page->w)/2);
      int yoffset=round((ymax-page->h)/2);     
      for(vector<dotcoint>::iterator i=screenvec.begin(); 
i!=screenvec.end(); i++) {
          if (((*i).x>-1)&&((*i).y>-1)&&((*i).x<xmax)&&((*i).y<ymax)) 
circlefill(page,(*i).x-xoffset,(*i).y-yoffset,dotradius,fgcolor);
      }
      frameinfoP->set_framebmp_ptr(page);
    }
  }

/* SHOW THE BITMAPS - no problem here*/
  BITMAP* next_bmp=NULL;
  frameinfo* nextframeinfoP=(*frameinfovecptr)[0];
  next_bmp=nextframeinfoP->get_framebmp_ptr();
  for (int i=0; i<frameinfovecptr->size(); i++) {
    frameinfo* frameinfoP=(*frameinfovecptr)[i];
    if (i!=frameinfovecptr->size()-1) {
      nextframeinfoP=(*frameinfovecptr)[i+1];
      next_bmp=nextframeinfoP->get_framebmp_ptr();
    }
    else {
      next_bmp=resp_bmp;
    }
  }

/* SHOW RESPONSE SCREEN - no problem here */
  trialp->response=record_response(resp_bmp,respvecptr,dot);

/* DESTROY THE BITMAPS - problems! */
  destroy_bitmap(resp_bmp); resp_bmp=NULL;
  if (fixpage!=NULL) {
    destroy_bitmap(fixpage);  fixpage=NULL;
  };
  if (dot!=NULL) { 
    destroy_bitmap(dot);  dot=NULL; 
  };
  if (respvecptr!=NULL) {  
    delete respvecptr;   respvecptr=NULL; 
  };
  for (int i=0; i<frameinfovecptr->size(); i++) { // clean up the bitmaps
    frameinfo* frameinfoP=(*frameinfovecptr)[i];
    if (frameinfoP->get_framebmp_ptr()!=NULL) {
      if (frameinfoP->get_lifetime()!=experiment_life) {
        BITMAP* bmp_ptr=frameinfoP->get_framebmp_ptr();
        destroy_bitmap(bmp_ptr);
        frameinfoP->set_framebmp_ptr(NULL);
      };
    }
  }
  return 0;
}

- Raw text -


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