Discussion:
HeapAlloc Crashes in wcsncpy
(too old to reply)
Rohit
2008-10-17 06:30:35 UTC
Permalink
I am using a procedure which calls HeapAlloc to allocate memory.

void myAlloc(DWORD Size)
{
if ( Size > 0 ) {
char *Temp;
Temp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (DWORD) Size);
return Temp;
}
else
return (void *)0;
}

And the call to this function is :

myVar = (structname *)myAlloc( sizeof(structvar));

This sometimes gives me a crash and analysing Dr.Watson logs, stack
can be traced as

ntdll!wcsncpy+0x74f

for call to : myAlloc->wcsncpy

Can anyone comment ?
Giovanni Dicanio
2008-10-17 06:48:53 UTC
Permalink
Post by Rohit
I am using a procedure which calls HeapAlloc to allocate memory.
void myAlloc(DWORD Size)
{
if ( Size > 0 ) {
char *Temp;
Temp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (DWORD) Size);
return Temp;
}
else
return (void *)0;
}
Of course, the above code is not compilable (should return void*) - is that
the *actual* code?
Post by Rohit
myVar = (structname *)myAlloc( sizeof(structvar));
This is not clear.
What is 'structname' ?
What is 'structvar' ?

Posting compilable code with more context information is better than force
"guessing"...

Giovanni
Ulrich Eckhardt
2008-10-17 07:01:32 UTC
Permalink
Post by Rohit
I am using a procedure which calls HeapAlloc to allocate memory.
void myAlloc(DWORD Size)
This function returns nothing...
Post by Rohit
{
if ( Size > 0 ) {
char *Temp;
Temp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (DWORD) Size);
return Temp;
}
else
return (void *)0;
}
... but has return statements with an argument. Post your real code,
anything else is just useless guessing from here on. In any case though,
take care of the following points first:
1. Tell us what language you are using.
2. Avoid casts. In C, they should rarely be necessary, in C++ using C-styl
casts is usually wrong.
Post by Rohit
myVar = (structname *)myAlloc( sizeof(structvar));
FYI: 'sizeof' yields a 'size_t', which is also the argument that an
allocation function should take. Further, I sense a disagreement between
the type you cast to and the size requested.
Post by Rohit
for call to : myAlloc->wcsncpy
'myAlloc' is a function, so that expression makes zero sense.

Provide relevant info, to make it possible for you to get help.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Rohit
2008-10-20 07:12:52 UTC
Permalink
Post by Ulrich Eckhardt
Post by Rohit
I am using a procedure which calls HeapAlloc to allocate memory.
void myAlloc(DWORD Size)
This function returns nothing...
Post by Rohit
{
  if ( Size > 0 ) {
    char  *Temp;
    Temp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (DWORD) Size);
    return Temp;
  }
  else
    return (void *)0;
}
... but has return statements with an argument. Post your real code,
anything else is just useless guessing from here on. In any case though,
1. Tell us what language you are using.
2. Avoid casts. In C, they should rarely be necessary, in C++ using C-styl
casts is usually wrong.
Post by Rohit
myVar = (structname *)myAlloc( sizeof(structvar));
FYI: 'sizeof' yields a 'size_t', which is also the argument that an
allocation function should take. Further, I sense a disagreement between
the type you cast to and the size requested.
Post by Rohit
for call to : myAlloc->wcsncpy
'myAlloc' is a function, so that expression makes zero sense.
Provide relevant info, to make it possible for you to get help.
Uli
--
C++ FAQ:http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Ya .. it was a typo mistake .. Its void* instead of void .

typedef struct structname {
int SignalCode;
strcutname Pre, Suc;
int Sender;
void *SigP;
int SignalId;
int IsTimer;
int TimerTime;
int TimerToSetOrReset;
bool (* yEq)();
bool TestParams;
strctname Param;
} structvar;

for xAlloc - wcsncpy i meant that drwatson crash dump stack analysis
shows call to myAlloc then crashing in wcsncpy. This code is written
as a part of a test.c file.
Rohit
2008-10-20 07:15:31 UTC
Permalink
Ya .. it was a typo mistake .. Its void* instead of void .

typedef struct structname {
int SignalCode;
strcutname Pre, Suc;
int Sender;
void *SigP;
int SignalId;
int IsTimer;
int TimerTime;
int TimerToSetOrReset;
bool (* yEq)();
bool TestParams;
strctname Param;
} structvar;

for xAlloc - wcsncpy i meant that drwatson crash dump stack analysis
shows call to myAlloc then crashing in wcsncpy. This code is written
as a part of a test.c file.
Giovanni Dicanio
2008-10-20 07:58:38 UTC
Permalink
Post by Rohit
Ya .. it was a typo mistake .. Its void* instead of void .
typedef struct structname {
int SignalCode;
strcutname Pre, Suc;
What is 'strcutname' ?

Or is it another "typo", and you meant something like:

struct structname * Pre;
Post by Rohit
strctname Param;
What is 'strctname'?
Post by Rohit
This code is written
as a part of a test.c file.
Sorry, but the lines you posted are useless.

As already was written, you should post *compilable* code that we may
analyze to try to help.

Giovanni

Loading...