Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Memory overwrite

Started by King of Men Oct 19, 2008 at 6:07 PM 0 replies 900+ views
Original Post
King of Men
King of Men
I have some very strange behaviour. I have this code:

void makeLocalResolution (char* pre, RooRealVar& timeVariable, RooRealVar& localBias) {
  static int pdfs = 0;
  pdfs++;
  static char* pdfnames = new char[200];

  sprintf(pdfnames, "%s_ssf1_%i", pre, pdfs);
  RooRealVar* ssf1 = new RooRealVar(pdfnames, "", 0.9, 0.2, 5);
  sprintf(pdfnames, "%s_ssf2_%i", pre, pdfs);
  RooRealVar* ssf2 = new RooRealVar(pdfnames, "", 2.8, 0.2, 7);

  sprintf(pdfnames, "%s_gauss1_%i", pre, pdfs);
  RooGaussModel* gauss1 = new RooGaussModel(pdfnames, "", timeVariable, localBias, *ssf1);
  sprintf(pdfnames, "%s_gauss2_%i", pre, pdfs);
  RooGaussModel* gauss2 = new RooGaussModel(pdfnames, "", timeVariable, localBias, *ssf2);
  sprintf(pdfnames, "%s_frac_%i", pre, pdfs);
  RooRealVar* frac1 = new RooRealVar(pdfnames, "", 0.175, 0.02, 1);

  (...)
}

Ok, this is just creating a bunch of objects, with some bells and whistles to give them unique names for when I call the method more than once. This is needed in some cases, but is currently turned off for my debugging effort, so this code runs only once. Now, when I use gdb to step through this code, looking at the variable addresses for each line, everything looks fine until I come to the last 'new' statement. At that point I get this:
(gdb) p ssf1
$8 = (class RooRealVar *) 0xa5fea38
(gdb) p frac1
$9 = (class RooRealVar *) 0xa5fea38
In other words, the 'frac1' object is being created right on top of my 'ssf1' object! Naturally this creates all sorts of problems later in the program. Honestly I'm surprised it runs at all, but it does. My question: Does anyone have any ideas on how this kind of situation can occur? I have no idea how to debug this. Even if I can perhaps avoid this particular problem by moving my code around a bit, I would like to be confident that nothing similar is happening elsewhere in the program. I would really, really welcome suggestions.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.