Serious Problem using Dplot

Q&A for C, C++, and/or C# developers using DPlot

Moderator: DPlotAdmin

Post Reply
ecas
Posts: 4
Joined: Sun Feb 08, 2009 7:45 pm

Serious Problem using Dplot

Post by ecas »

hello david. i'm experiencing a serious error when using Dplot to do a 3D surface. It's not easy to explain, and i've tried basically everything so i'll just show you some code to see if you could help me.

Just some important info: i'm using VS 2005 c++ express edition, and running vista32 business, programing in C.
i'm copied the dplotlib.lib and the dplotlib.dll to my project folder.
i have dplot installed on c:\programfiles\dplot\.

My project is this: receive a 1023 bits signal, sample it with 5000 samples.
after this, use Correlation in the time domain and in the freq domain correlating this 5000 signal with local replicas stored (41 diffferent replicas).

Basically i 1st create two 2D arrays with [41][5000]

Code: Select all

double *array_time[41], *array_freq[41]; (global variables)

double *result_time[41]; //correlation result, explained later

Then on the load method, i fill them with my data:

Code: Select all

for(a=0; a<41;a++){ //41 lines, each one with 5000 doubles
	array_time[a] = (double*) malloc(sizeof(double) * 5000); 
}
for(a=0; a<41;a++){
array_freq[a] = (double*) malloc(sizeof(double) * 5000);
}

//then i call both loaders:
time_loader(array_time,signal_array);
freq_loader(array_freq,signal_array);
After loading, i'm in this moment only using Time Domain Correlation so:
The actual method:

Code: Select all

//create the 2D array to store the results
//it will only store the Z component
for(i = 0; i != 41; i++)
{
           result_time[i] = (double*) malloc(sizeof(double) * 5000);
}

//correlation method
for(i=0; i <41 ; i++){

		for (k = 0; k < 5000; k++)
		{
			result_time[i][k] = timeCorrelation(sample_signal,array_time[i],sample_array_size, k);
		
		}
	}

i've tested everything on my code to this part. If i save the result_time[][] values to a .txt file, everything is correct, so i imagine that the problem isn't here. (it stores the correct values, even when the program crashes)

After this method i wanna use the result_time[][] to plot a 3D graph using the Dplot, so i call:

Code: Select all

timeGraph(result_time);
The complete graph class is this:

Code: Select all

#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include "dplot.h"

#define DOUBLEPRECISION
#ifdef DOUBLEPRECISION
#define REAL double
#define DPLOT_PLOT dPlot_Plot8
#else
#define REAL float
#define DPLOT_PLOT dPlot_Plot
#endif


void timeGraph(double *result_time[] ){
	DPLOT	DPlot;
	int		DocNum;
	int		Nx, Ny;
	REAL	extents[4];


#ifdef __GNUC__
	HINSTANCE dll;
	if((dll = dPlot_Init()) == NULL)
	{
		printf("LoadLibrary for DPLOTLIB.DLL failed.\n");
		exit(0);
	}
#endif
	dPlot_SetErrorMethod(2);


	Nx = 41;	// Columns
	Ny = 5000;	// Rows

	extents[0] = -10.f;		// xlo
	extents[1] = 0.f;		// ylo
	extents[2] = 10.f;		// xhi
	extents[3] = 5000.f;	// yhi
	
	memset(&DPlot,0,sizeof(DPlot));
	DPlot.Version		= DPLOT_DDE_VERSION;
	DPlot.DataFormat	= DATA_3D;
	DPlot.MaxCurves		= Nx-1;		// Size of the grid, in grid cells
	DPlot.MaxPoints		= Ny-1;
	DPlot.NumCurves		= 1;
	DPlot.Scale			= SCALE_LINEARX_LINEARY;
	strcpy(DPlot.Title[0],"Time Correlation");
	strcpy(DPlot.XAxis,"Doppler Shift");
	strcpy(DPlot.YAxis,"Code Phase");	 

	DocNum = DPLOT_PLOT(&DPlot,extents,*result_time,"[Caption(\"GuiCorrelation\")]"
		"[Contour3D(1)][ContourMethod(0)][ContourGrid(1)][ContourAxes(1)][ContourView(315,26)]"
		"[ContourLevels(20,-1000,5000)][ContourScales(250,1,0.5)][ContourLighting(2,0.2)]"
		"[FontPoints(1,10)][ZAxisLabel(\"Magnitude\")]" );


#ifdef __GNUC__
	FreeLibrary(dll);
#endif

}

ok so now my actual problem:

i was until this point only using the Time Correlation, but when i created the array_time, i created also the array_freq although i was not using it until now.

The strange thing is this: if i create array_freq with less than 5000 doubles, the program gives me an exception when i call the Dplot function.

this is very strange because the graphic is done with another array, (result_time[][]) and i don't use array_freq for nothing (will use in the future). i just create it and then free() it when the app closes.

The error is this:
Unhandled exception at 0x77588a73 in GUI_Correlation.exe: 0xC0000005: Access violation reading location 0x02809000.


This is the stack:
>ntdll.dll!77588a73()
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
user32.dll!769763d4()
dplotlib.dll!00022b81()
dplotlib.dll!00023d0e()
>GUI_Correlation.exe!timeGraph(double * * result_time=0x0012b9d8) Line 61 + 0x1e bytes


my hope is that i'm doing something wrong on the graph method and you can spot it.
So resuming: i use array_time[][] to calculate the correlation values and then store it on result_time[][].
i then pass result_time[][] to Dplot so it makes the graph.

If i create array_freq with 5000 doubles or more, it all works fine.
If i don't create array_freq, or create it with less than 5000 doubles the programs crashes on the DocNum = DPLOT_PLOT (&DPlot,extents,*result_time,..... statement.


i know this is a very long post and if you can help me out it would be great.
Thank you
User avatar
DPlotAdmin
Posts: 2312
Joined: Tue Jun 24, 2003 9:34 pm
Location: Vicksburg, Mississippi
Contact:

Post by DPlotAdmin »

The only thing that stands out to me is this:
for(i = 0; i != 41; i++)
{
result_time = (double*) malloc(sizeof(double) * 5000);
}


I don't think there's any guarantee that those 41 1D arrays will be contiguous - maybe they are, maybe they aren't. The DPlot_Plot call is looking for a single contiguous array, and if that isn't what you're providing then DPlot will be accessing a bogus address.
Visualize Your Data
support@dplot.com
ecas
Posts: 4
Joined: Sun Feb 08, 2009 7:45 pm

Post by ecas »

hey thanks for the reply, but i think i managed to isolate the problem.

could you explain this better to me:

Code: Select all

//this is my code. i copied it from ctest.c if i remember correctly

REAL	extents[4];


extents[0] = -10.f;		// xlo
	extents[1] = 0.f;		// ylo
	extents[2] = 10.f;		// xhi
	extents[3] = 5000.f;	// yhi

My call to Dplot:

Code: Select all

DocNum = DPLOT_PLOT(&DPlot,extents,*result_time,
on the DPLOTLIB user guide you have this:
int DPlot_Plot8(DPLOT *DPlot, double *array1, double *array2, LPSTR commands);


i changed REAL extents[4] to REAL extents[41] and it works sometimes and others dont.

could you explain me better what i sould pass in DPLOT_PLOT() function?


Thank you
User avatar
DPlotAdmin
Posts: 2312
Joined: Tue Jun 24, 2003 9:34 pm
Location: Vicksburg, Mississippi
Contact:

Post by DPlotAdmin »

i changed REAL extents[4] to REAL extents[41] and it works sometimes and others dont.
Either should work, always (though [41] will never be necessary). My guess is if you run into problems with either [4] or [41] it is due to the way your result_time array is arranged, and nothing to do with the extents array.

Your result_time array should be one array allocated all at once. As far as I know that is the only way to ensure that element [j] always immediately follows element [j-1] in memory, for all j.

Instead of multiple calls to malloc you'd use

Code: Select all

double *result_time;

result_time = (double *)malloc(41*5000,sizeof(double));
and when filling the array you'd use:

Code: Select all

for(i=0; i <41 ; i++){

      for (k = 0; k < 5000; k++)
      {
         result_time[i*5000+k] = timeCorrelation(sample_signal,array_time[i],sample_array_size, k);
      
      }
   }
Visualize Your Data
support@dplot.com
ecas
Posts: 4
Joined: Sun Feb 08, 2009 7:45 pm

Post by ecas »

hey david, finally figured it out :D

i has being stupid from the start:

i was trying to pass a 2D array as an argument to the Dplot function.

all i had to do was transform my 2D array into 1D array and pass it.

My bad. 8)

you may delete the thread if you want

BTW: very nice program, congratulations and thank you once again
Post Reply