|
|
 |
 |
 |
 |
realloc() / free() BYTE* immediately after malloc() fails
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
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
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; > } > // 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 -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask
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; > } > // 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. -- Erik Wikstrm
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
"attibln" <Atti @gmx.net> wrote in message news:1181138591.724555.306630@q69g2000hsb.googlegroups.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; > } > // 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. -- Fred L. Kleinschmidt
On 6 Jun., 16:25, "Victor Bazarov" <v.Abaza@comAcast.net> wrote: > 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
On 6 Jun., 16:31, Erik Wikstrm <Erik-wikst@telia.com> wrote: > 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.
On 6 Jun., 16:37, "Victor Bazarov" <v.Abaza@comAcast.net> wrote: > 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! :)
On 6 Jun., 16:37, "Fred Kleinschmidt" <fred.l.kleinmschm @boeing.com> wrote: > 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);
attibln wrote: > On 6 Jun., 16:37, "Fred Kleinschmidt" > <fred.l.kleinmschm @boeing.com> wrote: >> 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
On 6 Jun., 19:24, "Victor Bazarov" <v.Abaza@comAcast.net> wrote: > > 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 <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.)
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
On Jun 6, 4:25 pm, "Victor Bazarov" <v.Abaza@comAcast.net> wrote: > 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:james.ka@gmail.com Conseils en informatique oriente objet/ Beratung in objektorientierter Datenverarbeitung 9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
On Jun 6, 7:37 pm, attibln <Atti@gmx.net> wrote: > On 6 Jun., 19:24, "Victor Bazarov" <v.Abaza @comAcast.net> wrote: > > > 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 <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. -- James Kanze (GABI Software) email:james.ka@gmail.com Conseils en informatique oriente objet/ Beratung in objektorientierter Datenverarbeitung 9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
|
 |
 |
 |
 |
|