psplot.h with ubuntu


dojo
04-10-2008, 04:22 AM
Hi!

I am using ubunt and the gcc-Version 4.1.2 compiler for generating the executables. In psplot.h I needed to change line 78:
strcpy(cmd,"\"gv\" ");

to get ghostview running. I wrote a small program xscrsho.cpp:

#include "nr3.h"
#include "psplot.h"
#include "scrsho.h"

float banana(float x)
{
float value;
value = -x*x+x*x*x;
return value;
}

int main(void)
{
scrsho(banana);
return 0;
}

I can compile it and run it and the gv window pops up and displays the function in the specified range. However, if I enter a second range, nothing happens. If I close the gv window the program ends with a segmentation fault.

I assume, that problem occurs within the close() method in psplot.h. Do you have any idea why this is happening?

dojo

davekw7x
04-10-2008, 02:07 PM
Hi!... the program ends with a segmentation fault.

Because of an unfortunate design decision in psplot, it turns out that it tries to close the file twice.

Try the following: Change the PSplot constructor in psplot.h (starting at about line 99) as shown (add the lines with blue comments).

PSplot(PSpage &page, Doub ppll, Doub ppur, Doub qqll, Doub qqur)
: PSpage(page.PLT,page.file),
pll(ppll), qll(qqll), pur(ppur), qur(qqur),
xll(ppll), yll(qqll), xur(ppur), yur(qqur), xbox(4), ybox(4),
majticsz(8.), minticsz(4.) {
page.PLT = NULL; // zero it out in the base class
page.file = NULL; // zero it out in the base class
strcpy(fontname,page.fontname);
fontsize = page.fontsize;
setlimits(xll,xur,yll,yur);
}


Also, there is a memory leak that can be fixed by changing the destructor for PSpage (line 21 in psplot.h) to

~PSpage() {if (PLT) close(); if (file) delete [] file;}


There are other things that could be changed, but these seem to be the minimal changes that make it work for me (using kghostview on Centos 5 Linux).

Regards,

Dave

Footnote: Although this workaround seems to make the example work with scrsho(), it screws up other examples that just use PSplot objects to write to files. Other changes are necessary, I'm thinking.

In other words: I suggest that you make your own private copy/copies of any Recipes files that you want to experiment with and leave the originals alone. The whole point is that what works for me may not be suitable for your use. (Your Mileage May Vary.)

Bill Press
05-01-2008, 08:54 PM
After some off-line discussion with davekw7x, we think that a better solution (also suggested by him) is described here:
http://www.nr.com/forum/showthread.php?t=1446