1

I am building an application consisting of the following, separate modules: - GAClient: a C++ executable - GAOrcl: a C DLL generated by Oracle Pro*C - GAEngine: a C++ DLL - MyGAUtils: a C++ library of functions

Here's how the different modules are related: - GAClient calls several functions from GAOrcl, and a few from MyGAUtils; - GAOrcl calls a few functions from GAEngine ("DoGATraining" is one of them) - GAEngine calls several functions from MyGAUtils

I have two similar instructions in GAClient.cpp:

 double* vKaz=(double*)malloc(5*sizeof(double*));

and GAEngine.cpp:

double* vPastTarget=(double*)malloc(5*sizeof(double*)); 

My problem is, malloc works fine when called from GAClient, but subsequently crashes when called from GAEngine. Visual Studio debugger throws a "GAClient.exe has triggered a breakpoint" arror, and points me to a "lseeki64.c" source file, which I have no idea what is...

I suspect this might have something to do with the fact that DoGATraining is defined as an extern "C":

#define EXPORT __declspec(dllexport)
extern "C" EXPORT int       __stdcall DoGATraining(int pPastDataCount, double* pPastData)

Any idea where I might start troubleshooting?

gcaglion
  • 131
  • 1
  • 12

1 Answers1

5

You want an array of 5 doubles, not an array of 5 pointers to double, so change

double* vKaz=(double*)malloc(5*sizeof(double*));

to

double *vKaz = malloc(5 * sizeof(double)); /* don't cast malloc */

or

double *vKaz = malloc(5 * sizeof(*vKaz));
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 1
    For reference, [don't cast malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – isedev Oct 02 '14 at 09:21
  • Thanks @AlterMann, that was indeed a problem, however the behavior is not changed; the debugger still triggers a breakpoint and jumps to this (to me) obscure "lseeki64.c" ... – gcaglion Oct 02 '14 at 09:27
  • @gcaglion, can you show the call to `DoGATraining`? – David Ranieri Oct 02 '14 at 09:32
  • int BestCid; BestCid=DoGATraining(pTotalBars, HVal); if(BestCid<0) return -1; – gcaglion Oct 02 '14 at 09:34
  • `HVal` is defined as `double *HVal;`? – David Ranieri Oct 02 '14 at 09:36
  • @isedev, I am a beginner C/C++ programmer, but I've read about not casting malloc several times; my problem with that is, when bulding in C++ from Visual Studio, omitting the cast generates a compile error (C2440, cannot convert from 'void*' to 'double*') – gcaglion Oct 02 '14 at 09:37
  • I suggest you to tag this question `c` `c++` in order to get more help – David Ranieri Oct 02 '14 at 09:44
  • 1
    Make sure you are not allocating memory on one side of a DLL boundary and freeing it on the other – M.M Oct 02 '14 at 09:48
  • @MattMcNabb I'm not sure how to do that, the only thing that comes to mind is a logfile that gets opened and closed by the dll , and the call to DoGATraining is in between. I've then tried to fclose the file before calling DoGATraining, and even disabled logging all along, and the only thing that chamges is the (obscure) source file the debugger throws me to (this time is something called time.inl) – gcaglion Oct 02 '14 at 10:07
  • Some Update: I noticed in the Output tab the following message: "Critical error detected c0000374" , which seems to be in line with @MattMcNabb 's comment; still, no luck in finding the root cause of the problem. – gcaglion Oct 02 '14 at 10:30
  • Note: Slightly more simple: `double *vKaz = malloc(5 * sizeof *vKaz);`, but the then that is almost a style issue. – chux - Reinstate Monica Oct 02 '14 at 13:10
  • I've been doing some more tests, and, interestingly, it seems once the execution gets to the GAEngine module, every possible call to malloc() triggers the same error; I've even tried a simple char* kkk=malloc(30); , and the outcome is the same... – gcaglion Oct 02 '14 at 13:42