By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
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

Jun 6 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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;
}
------------------------------------------------------------------
instead of realloc() makes the program print

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

Jun 6 '07 #2

P: n/a
attibln wrote:
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 <cstdlib>.
if( byP_jpeg_file == NULL )
{
wcout << L"eek - allocating memory failed";
return 1;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #3

P: n/a
On 2007-06-06 16:03, attibln wrote:
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;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
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.

--
Erik Wikström
Jun 6 '07 #4

P: n/a
attibln wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #5

P: n/a

"attibln" <At*****@gmx.netwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
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;
}

// PROBLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE M
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.
--
Fred L. Kleinschmidt
Jun 6 '07 #6

P: n/a
On 6 Jun., 16:25, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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

Jun 6 '07 #7

P: n/a
On 6 Jun., 16:31, Erik Wikström <Erik-wikst...@telia.comwrote:
>
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.
This I surmise from the fact that it compiles and runs perfectly
fine for me on the same version of VS.
Jun 6 '07 #8

P: n/a
On 6 Jun., 16:37, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
attibln wrote:
addition:
this
------------------------------------------------------------------
int i_error;
errno = 0; // reset
free( byP_jpeg_file );
if( errno =! 0 )

:-)

You just wrote here

if (errno = !0)
Ouch, that was a (bad) typo.

Thanks Victor!
Thanks everyone! :)

Jun 6 '07 #9

P: n/a
On 6 Jun., 16:37, "Fred Kleinschmidt"
<fred.l.kleinmschm...@boeing.comwrote:
>
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);

Jun 6 '07 #10

P: n/a
attibln wrote:
On 6 Jun., 16:37, "Fred Kleinschmidt"
<fred.l.kleinmschm...@boeing.comwrote:
>>
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);
That's a good one! I didn't even see any 'fread' in your code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #11

P: n/a
On 6 Jun., 19:24, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>
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.
fread.c is probably included by stdafx.h or <iostreamor 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.)

Jun 6 '07 #12

P: n/a
attibln wrote:
[..]
(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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #13

P: n/a
On Jun 6, 4:25 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
attibln wrote:
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.
He's asking about a specific C++ problem, though.

[...]
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).
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 7 '07 #14

P: n/a
On Jun 6, 7:37 pm, attibln <Atti...@gmx.netwrote:
On 6 Jun., 19:24, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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.
fread.c is probably included by stdafx.h or <iostreamor 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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 7 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.