Thursday, October 26, 2006

Today I am the perp. Today the mighty have fallen.

Or at least I have fallen - I'm not mighty, I'm wee.

So I'm trying to figure out why, in my C++ program, I'm getting an access violation.

I mean, c'mon! I know I write, if not perfect, then at least pretty darn good code, and I've really been paying attention to this particular unit and I know there are no mistakes.

Yet, there's still a pesky A/V. BAD!

Step through the code. Ok, now we're getting some place.
Well, that's weird, that's where I thought I would get an error.
Hmm... keep going.
Uh huh, right, ok, yeah...
DOH!
TDateObject* __fastcall UserData::FindDate( TDate _Date )
{
TDateObject* Result = 0;

for ( int ix = 0; ix < Count; ix++ )
{
if ( Items[ix]->Date == _Date )
{
Result = Items[ix];
break;
}
}

if ( Result == 0 )
{
Result = new TDateObject();
TObjectList::Add( Result );
}
}

Even though the moral of today's story should obviously be obvious, it obviously wasn't obvious enough for me to obviously catch it whilst obviously programming it.

Moral:
If you're going to use a function to create an object (or any memory for that matter), you'd better make darn tootin' sure you're actually returning something.

I'm a moron.
That is all.

1 Comments:

Blogger Tim said...

I am a moron.
That is all.

Fixed.

2:11 PM  

Post a Comment

<< Home