memory problems | | |
I've run into an interesting memory problem. I think I'm running out of
heap space, but I'm not sure....
I'm creating two new arrays like such....
pImage = new UBYTE [nImageSize];
pImageTemp = new UBYTE [nImageSize];
where nImageSize = 262144. new is NOT returning NULL at this point, which
gives me the impression that new succeeded.
After a little error checking, the code goes to the following statement....
for(int tries = 0;tries <10 && fread (pImageTemp, sizeof (UBYTE),
nImageSize, pFile) != nImageSize; tries++)
{
fclose (pFile);
if (tries == 9)
{
delete [] pImage;
delete [] pImageTemp;
return 0;
}
Now given a pFile named "cloak.tga" (which BTW, was opened succesfully),
this code will successfully load "cloak.tga" most of the time. However, at
an arbitrary point ( say the 4th or 5th time I call this code) fread will
fail. Once it fails it enters the above loop to try freading 9 more times.
Once it fails the first time it will ALWAYS fail the next 9 times. At this
point, when
delete [] pImageTemp;
is called, I get the following error message:
HEAP[Bruteball.exe]: Heap block at 070ADB18 modified at 070ADF50
past requested size of 430
So am I running out of Heap space? If so, why isn't new returning NULL?
Anyone have any ideas of how to fix this issue?
Thanks,
-Pete | | | | re: memory problems
Pete <psl@usa.com> wrote in message news:4wg_a.130997$Ho3.16611@sccrnsc03...[color=blue]
> I've run into an interesting memory problem. I think I'm running out of
> heap space, but I'm not sure....
>
> I'm creating two new arrays like such....
>
> pImage = new UBYTE [nImageSize];
> pImageTemp = new UBYTE [nImageSize];
>
> where nImageSize = 262144. new is NOT returning NULL at this point,[/color]
which[color=blue]
> gives me the impression that new succeeded.
>
> After a little error checking, the code goes to the following[/color]
statement....[color=blue]
>
> for(int tries = 0;tries <10 && fread (pImageTemp, sizeof (UBYTE),
> nImageSize, pFile) != nImageSize; tries++)
> {
> fclose (pFile);
> if (tries == 9)
> {
> delete [] pImage;
> delete [] pImageTemp;
> return 0;
> }
>
> Now given a pFile named "cloak.tga" (which BTW, was opened succesfully),
> this code will successfully load "cloak.tga" most of the time. However,[/color]
at[color=blue]
> an arbitrary point ( say the 4th or 5th time I call this code) fread will
> fail. Once it fails it enters the above loop to try freading 9 more[/color]
times.[color=blue]
>
> Once it fails the first time it will ALWAYS fail the next 9 times.[/color]
But you are closing the file in the loop, so it has to fail every other
time.
[color=blue]
> At this
> point, when
>
> delete [] pImageTemp;
>
> is called, I get the following error message:
>
> HEAP[Bruteball.exe]: Heap block at 070ADB18 modified at 070ADF50
> past requested size of 430
>
> So am I running out of Heap space? If so, why isn't new returning NULL?[/color]
Unless it's an old compiler it will never return null. 'new' throws an
exception if it fails, so if it returns at all it succeeded.
[color=blue]
> Anyone have any ideas of how to fix this issue?[/color]
I don't think you are running out of memory. It looks like a memory
corruption caused by writing to where you are not supposed to. I can't tell
from the code you've posted what's wrong. Try deleting the memory
immediately after allocating it and see if the error still occurs. If it
doesn't then you know you are doing something later that's causing the
corruption. If you post the code for a complete, small-as-possible program
that exhibits the problem then the cause of the problem might be more
apparent.
[color=blue]
>
> Thanks,[/color]
Thank you. I'm sure many regulars are relieved and grateful for your
on-topic question.
DW | | | | re: memory problems
LOL! Good point about the fclose(). I just added that retry loop last
minute to try and debug. Course if your debugging code has a bug in it, its
not that useful, LOL!
Thanks for the feedback. I managed to figure out what was wrong. This code
is used to colorize and overlay a bunch of grayscale tga files into the same
memory chunk, so that a single openGL texture can be created out of about
15 overlaped tga files. Its for a random anime face generator. Turns out
at some point I got the brilliant idea to save space by turning a couple all
ALPHA tga files (bald guys) into 16x16, in my 256x256 image directory. Of
course trying to fit a 256x256 image in the space of a 16x16 image
apparently causes some problems ;) Funny thing is I didn't make the same
"optimization" in my 64x64 image dirrectory, so I wasn't having a problem
with the smaller images. LOL! I'm a moron.
Thanks again for the feedback.
-Pete
"David White" <no@email.provided> wrote in message
news:PVg_a.799$d6.64498@nasal.pacific.net.au...[color=blue]
> Pete <psl@usa.com> wrote in message[/color]
news:4wg_a.130997$Ho3.16611@sccrnsc03...[color=blue][color=green]
> > I've run into an interesting memory problem. I think I'm running out of
> > heap space, but I'm not sure....
> >
> > I'm creating two new arrays like such....
> >
> > pImage = new UBYTE [nImageSize];
> > pImageTemp = new UBYTE [nImageSize];
> >
> > where nImageSize = 262144. new is NOT returning NULL at this point,[/color]
> which[color=green]
> > gives me the impression that new succeeded.
> >
> > After a little error checking, the code goes to the following[/color]
> statement....[color=green]
> >
> > for(int tries = 0;tries <10 && fread (pImageTemp, sizeof (UBYTE),
> > nImageSize, pFile) != nImageSize; tries++)
> > {
> > fclose (pFile);
> > if (tries == 9)
> > {
> > delete [] pImage;
> > delete [] pImageTemp;
> > return 0;
> > }
> >
> > Now given a pFile named "cloak.tga" (which BTW, was opened succesfully),
> > this code will successfully load "cloak.tga" most of the time. However,[/color]
> at[color=green]
> > an arbitrary point ( say the 4th or 5th time I call this code) fread[/color][/color]
will[color=blue][color=green]
> > fail. Once it fails it enters the above loop to try freading 9 more[/color]
> times.[color=green]
> >
> > Once it fails the first time it will ALWAYS fail the next 9 times.[/color]
>
> But you are closing the file in the loop, so it has to fail every other
> time.
>[color=green]
> > At this
> > point, when
> >
> > delete [] pImageTemp;
> >
> > is called, I get the following error message:
> >
> > HEAP[Bruteball.exe]: Heap block at 070ADB18 modified at 070ADF50
> > past requested size of 430
> >
> > So am I running out of Heap space? If so, why isn't new returning[/color][/color]
NULL?[color=blue]
>
> Unless it's an old compiler it will never return null. 'new' throws an
> exception if it fails, so if it returns at all it succeeded.
>[color=green]
> > Anyone have any ideas of how to fix this issue?[/color]
>
> I don't think you are running out of memory. It looks like a memory
> corruption caused by writing to where you are not supposed to. I can't[/color]
tell[color=blue]
> from the code you've posted what's wrong. Try deleting the memory
> immediately after allocating it and see if the error still occurs. If it
> doesn't then you know you are doing something later that's causing the
> corruption. If you post the code for a complete, small-as-possible program
> that exhibits the problem then the cause of the problem might be more
> apparent.
>[color=green]
> >
> > Thanks,[/color]
>
> Thank you. I'm sure many regulars are relieved and grateful for your
> on-topic question.
>
> DW
>
>
>[/color] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|