Re: Program crashes before it starts!!!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



See comments inline.

"JC" <jeffreycameron@xxxxxxxxx> wrote in message
news:1152269916.128408.114360@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi all,

In response to your answers :

we basically use whatever Visual C++ puts in the DllMain function,
nothing fancy. I thought maybe one of the functions that composes the
DLL could have been the source of the problem.

Try to put the simplest code to dll:

.... DllMain(..)
{
return TRUE;
}

Both are using the Multi-Threaded Debug versions of the CRT (for debug)
and Multi-Threaded Release versions for Release.

We are using Visual Studio .NET 2003.

Another weird thing is that earlier, this error with
_CrtIsValidHeapPointer was occurring only when we called a function
that was inside the DLL. I thought this was perhaps because of some
string (char*) variables we pass to the DLL function being malloc'd
poorly so I switched to using arrays instead like this :

#define BUFFER_LENGTH 8192 /* its an 8k buffer, VERY large, the string
is not close to this size*/

It's very bad technique to allocate in stack huge arrays. In your case you
allocate it in the entry point so depending on the size of your stack
(default is 1 MB) it may cause stack overflow.

(in DLL)
int mydllfunc(char *string);

(before - crashing at function call)
int main(int argc, char *argv[])
{
char *mystr = NULL;

mystr = calloc(BUFFER_LENGTH, sizeof(char));
strcpy(mystr, argv[1]);
mydllfunc(mystr);
}

I would try to make sure that argv[1] is not longer then mystr. At least,
this line:

strcpy(mystr, argv[1]);

Is potential candidate for buffer overflows. Use StrCpyN instead.

(after - crashing on startup at first variable initialisation)
int main(int argc, char *argv[])
{
char mystr[BUFFER_LENGTH] = "";

strcpy(mystr, argv[1]);
mydllfunc(mystr);
}

This code is obviously simplified a bit but represents the problem.

Help!! :-)



Play with size of buffer. Does simple: char mystr[10]; causes the same
problem?

--
Vladimir


.



Relevant Pages

  • Re: Automatic variable is not destroyed?
    ... char* GetChar ... int main ... store automatic variables. ... return myStr; ...
    (comp.lang.c)
  • Re: Program crashes before it starts!!!
    ... DLL could have been the source of the problem. ... Both are using the Multi-Threaded Debug versions of the CRT ... string (char*) variables we pass to the DLL function being malloc'd ... int main ...
    (microsoft.public.win32.programmer.kernel)
  • DLL - prog crashes
    ... Hi i have been working on a prog that calls some functions from a dll. ... extern int iax_register(struct iax_session *session, char *server, char ...
    (microsoft.public.vc.mfc)
  • DLL - prog crashes
    ... Hi i have been working on a prog that calls some functions from a dll. ... extern int iax_register(struct iax_session *session, char *server, char ...
    (microsoft.public.vc.language)
  • Re: Exporting/importing a variable from a Dll
    ... > And in the dll I made a pointer to an integer, which allowed me to set it ... I could be wrong, but I think if you export an int*, then what you get back ... if you export a char* what you'll get back here will be a char**. ...
    (microsoft.public.vc.mfc)