realloc() / free() BYTE* immediately after malloc() fails

A

attibln

Hi,

I'm having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)

---------- START ----------
#include "stdafx.h"

#include <iostream>
using namespace std;

int main( int argc, char * argv[] )
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEM
realloc( byP_jpeg_file , 0 );

return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)

Please help!

~ Attila
 
A

attibln

addition:

this
------------------------------------------------------------------
int i_error;
errno = 0; // reset
free( byP_jpeg_file );
if( errno =! 0 )
{
wchar_t wct_error[256];
i_error = _wcserror_s( wct_error , 256 , errno );
if( i_error != 0 )
{
wcout << L"_wcserror_s() failed";
return 3;
}
wcout << wct_error;
return 2;
}
 
V

Victor Bazarov

attibln said:
Hi,

I'm having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)

Consider asking questions about debugging in VC++ in the newsgroup
dedicated to VC++. Take a look at "microsoft.public.vc.*" hierarchy.
---------- START ----------
#include "stdafx.h"

That's a non-standard header. Consider removing it (at least when
posting the code here).
#include <iostream>
using namespace std;

int main( int argc, char * argv[] )

You don't seem to be using 'argc' or 'argv'. Why declare them?
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );

'malloc' is undefined. Consider including said:
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEM
realloc( byP_jpeg_file , 0 );

'realloc' is undefined. Consider including <cstdlib>.

The behaviour of 'realloc' with 'size' passed as 0 is implementation
defined. You should consider asking in the newsgroup for your C++
compiler (see above).
return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)

Please help!

~ Attila

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi,

I'm having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)

---------- START ----------
#include "stdafx.h"

#include <iostream>
using namespace std;

int main( int argc, char * argv[] )
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEM
realloc( byP_jpeg_file , 0 );

return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)

Please help!

Either you have some weird settings somewhere else (why do you link ATL
statically when you don't use it?) or you have not shown us all the
code. This I surmise from the fact that it compiles and runs perfectly
fine for me on the same version of VS.
 
F

Fred Kleinschmidt

attibln said:
Hi,

I'm having an odd problem with a project using VisualStudio 2005 Prof.

When I execute the program below I get this message:

Debug Assertion Failed
Program: ...
File: dbgheap.c
Line: 1252
Expression: _CrtIsValidHeapPointer(pUserData)

---------- START ----------
#include "stdafx.h"

#include <iostream>
using namespace std;

int main( int argc, char * argv[] )
{
unsigned long lU_tile_size = 2956;
// read the tile data (jpeg file) into a buffer
unsigned char * byP_jpeg_file = (unsigned char *)
malloc( lU_tile_size );
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEM
realloc( byP_jpeg_file , 0 );

return 0;
}
---------- END----------

VS2005 settings:
win32 command line executable
ATL is linked statically
Multithreaded-Debug (/MTd)
runtime checking: both
optimisation is turned off (/Od)

Your debugger is broken. there are not 1252 lines in this code.
Also, you don't assign the result of realloc to anything.
 
V

Victor Bazarov

attibln said:
addition:

this
------------------------------------------------------------------
int i_error;
errno = 0; // reset
free( byP_jpeg_file );
if( errno =! 0 )

:)

You just wrote here

if (errno = !0)

which is the same as

if ((errno = 1) != 0)

.. Why are you surprised? The equality operator you're supposed to
use is spelled "not equal" or "!=". Fix it.
{
wchar_t wct_error[256];
i_error = _wcserror_s( wct_error , 256 , errno );
if( i_error != 0 )
{
wcout << L"_wcserror_s() failed";
return 3;
}
wcout << wct_error;
return 2;
}
------------------------------------------------------------------
instead of realloc() makes the program print

Operation not permitted.
means: errno == 1 == EPERM

V
 
A

attibln

Consider asking questions about debugging in VC++ in the newsgroup
dedicated to VC++. Take a look at "microsoft.public.vc.*" hierarchy.

ok, sry
int main( int argc, char * argv[] )

You don't seem to be using 'argc' or 'argv'. Why declare them?

That was just an example which contains the code where the problem
occurs. The example runs fine - so the problem must be connected with
the other stuff I do in the (real) project I'm working in. But I have
no idea what that could be. :(
'malloc' is undefined. Consider including <cstdlib>.
'realloc' is undefined. Consider including <cstdlib>.

these are probably included by #include "stdafx.h"
The behaviour of 'realloc' with 'size' passed as 0 is implementation
defined. You should consider asking in the newsgroup for your C++
compiler (see above).

ok
 
A

attibln

Either you have some weird settings somewhere else
(why do you link ATL statically when you don't use it?)
or you have not shown us all the code.

That's right, the source is about 179 KB big - 11 header and 11 cpp
files plus self-made libraries.
 
A

attibln

Your debugger is broken. there are not 1252 lines in this code.

File: dbgheap.c
Line: 1252

The line information refers to dbgheap.c which is part of the C
RunTime library VisualStudio uses.
Also, you don't assign the result of realloc to anything.

That's right - my fault again. Now doing this:

byP_jpeg_file = (BYTE *) realloc( byP_jpeg_file , 0 );

results in:

Debug Assertion Failed!
Program: ...
File: fread.c
Line: 93
Expression: (buffer != NULL)

that line (93) is
_VALIDATE_RETURN((buffer != NULL), EINVAL, 0);
 
V

Victor Bazarov

attibln said:
File: dbgheap.c
Line: 1252

The line information refers to dbgheap.c which is part of the C
RunTime library VisualStudio uses.


That's right - my fault again. Now doing this:

byP_jpeg_file = (BYTE *) realloc( byP_jpeg_file , 0 );

results in:

Debug Assertion Failed!
Program: ...
File: fread.c
Line: 93
Expression: (buffer != NULL)

that line (93) is
_VALIDATE_RETURN((buffer != NULL), EINVAL, 0);

That's a good one! I didn't even see any 'fread' in your code.

V
 
A

attibln

That's a good one! I didn't even see any 'fread' in your code.

fread.c is probably included by stdafx.h or <iostream> or VisualStudio
automatically.

Anyway, I wanted the buffer to be freed - which only didn't work
because of my typo.

(Fyi: When the free() didn't work because of the typo I tried to
deallocate the space by making it 0 bytes big.)
 
V

Victor Bazarov

attibln said:
[..]
(Fyi: When the free() didn't work because of the typo I tried to
deallocate the space by making it 0 bytes big.)

Fyi: making previously allocated array "0 bytes big" does not
necessarily perform actual deallocation. You're correct to use
'free' for that.

V
 
J

James Kanze

Consider asking questions about debugging in VC++ in the newsgroup
dedicated to VC++. Take a look at "microsoft.public.vc.*" hierarchy.

He's asking about a specific C++ problem, though.

[...]
'realloc' is undefined. Consider including <cstdlib>.
The behaviour of 'realloc' with 'size' passed as 0 is implementation
defined. You should consider asking in the newsgroup for your C++
compiler (see above).

The behavior he is seeing is not among the allowed behaviors;
all that is allowed is that either realloc return NULL, or that
it return a value "as if" the argument wasn't 0.

It's hard to say what the problem is, but most of the time,
these sort of errors are due to the free space arena being
corrupted---writing beyond the end of allocated memory, or
something like that. And finding such problems is difficult,
because the symptoms don't appear until long after the actual
error has occurred.
 
J

James Kanze

fread.c is probably included by stdafx.h or <iostream> or VisualStudio
automatically.
Anyway, I wanted the buffer to be freed - which only didn't work
because of my typo.
(Fyi: When the free() didn't work because of the typo I tried to
deallocate the space by making it 0 bytes big.)

In practice, there are only two reasons why free will fail:

-- the pointer you pass to it was not allocated by malloc, or
has been freed since, or

-- you've corrupted the free space arena.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top