Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[comp.lang.c.moderated] Crashes around NULL pointers

4 views
Skip to first unread message

Pranav Negandhi

unread,
Jul 4, 2008, 2:46:56 AM7/4/08
to
I'm getting my head around the C language by working on a Windows
extension for PHP. This problem relates to a non-Zend framework issue
that I'm facing.

I have to create a wrapper to the GD library to generate a thumbnail for
an image. This is the function I have written -

void _generate_thumbnail(char *sOriginal, char *sThumbnail, int w, int h)
{
gdImagePtr im;
FILE *fp;
unsigned int i;
char str[32];
int cbs;

im = gdImageCreateTrueColor(w, h);

if (im)
{
gdImageFilledRectangle(im, 2, 2, 80, 80, 0x50FF0000);
fp = fopen("c:\\myimage.png", "wb");

i = (unsigned int) &fp;
sprintf(str, "%u", i);
cbs = strlen(str);
WriteDebugLog("c:\\debug.log", str, cbs);

if (0 != fp)
{
gdImagePng(im, fp); // This is where it crashes
fclose(fp);
gdImageDestroy(im);
}
}
}


It always crashes on the line where I am writing the image to disk using
gdImagePng. I researched the error message ('Unhandled exception at
0x7c918fea in Apache.exe: 0xC0000005: Access violation writing location
0x00000010.') and learnt that it has something to do with a NULL pointer.

The gdImagePtr variable seems to be fine because the
gdImageFilledRectangle command succeeds. That leaves the file pointer.
But inspecting the value of the pointer reveals that it isn't NULL.

I'd appreciate someone shedding some light on what's going on here.

Regards,
Pranav
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Bob

unread,
Jul 8, 2008, 10:18:46 PM7/8/08
to
Pranav Negandhi wrote:

>
> It always crashes on the line where I am writing the image to disk
> using gdImagePng. I researched the error message ('Unhandled
> exception at 0x7c918fea in Apache.exe: 0xC0000005: Access violation
> writing location 0x00000010.') and learnt that it has something to do
> with a NULL pointer.
>
> The gdImagePtr variable seems to be fine because the
> gdImageFilledRectangle command succeeds. That leaves the file
> pointer. But inspecting the value of the pointer reveals that it
> isn't NULL.
>
> I'd appreciate someone shedding some light on what's going on here.
>

The only general rule is that the cause of a "crash" is code executed
at OR BEFORE the point where the crash is detected.

Any of the functions you are calling could be the culprit. Most of
those functions are non-standard, so you need to read the documentation
for those functions.

While it's not related to your crash, writing the address of a FILE
pointer to a debug log (in any form) strikes me as a pointless exercise.

Jasen Betts

unread,
Jul 10, 2008, 2:02:50 PM7/10/08
to
On 2008-07-04, Pranav Negandhi <pra...@pranavnegandhi.com> wrote:

> I'm getting my head around the C language by working on a Windows
> extension for PHP. This problem relates to a non-Zend framework issue
> that I'm facing.
>
> I have to create a wrapper to the GD library to generate a thumbnail for
> an image. This is the function I have written -

there is a GD module already written for PHP also an image magick module.
alternatively you can probably find a port of netpbm and communicate
with that using system....

ob_start() (etc...) can be handy when php functions send their output
in the wrong direction.

Bye.
Jasen

Jasen Betts

unread,
Jul 10, 2008, 2:02:56 PM7/10/08
to
On 2008-07-09, Bob <nos...@does.not.exist> wrote:

> While it's not related to your crash, writing the address of a FILE
> pointer to a debug log (in any form) strikes me as a pointless exercise.

It proves if it's NULL or not ... desperate times and all that :)

Bye.
Jasen

Bob

unread,
Jul 12, 2008, 7:10:30 PM7/12/08
to
Jasen Betts wrote:

> On 2008-07-09, Bob <nos...@does.not.exist> wrote:
>
> > While it's not related to your crash, writing the address of a FILE
> > pointer to a debug log (in any form) strikes me as a pointless
> > exercise.
>
> It proves if it's NULL or not ... desperate times and all that :)
>

Only to the paranoid. Following the declaration

FILE *fp;

&fp (which the code in the original post was printing out) is guaranteed
to be non-null.

0 new messages