Weird access violation when copying data

Started by
6 comments, last by Hodgman 8 years, 3 months ago

I'm trying to resize a bitmap for my 2d engine, however I keep getting an access violation at random points while I'm iterating through the bitmap. I really don't know what can be causing it. It comes up at random row/column and offset, can someone just look over my code and see what's wrong?

void ResizeBitmap(BITMAP_FILE* out, BITMAP_FILE* original, RECT resizeRectangle){//only works for 32 bit bitmaps for nowif (original->infoHeader.biBitCount != 32)return;int newXDimension = resizeRectangle.right - resizeRectangle.left;int newYDimension = resizeRectangle.bottom - resizeRectangle.top;int newDataSize = (original->infoHeader.biBitCount / 8) * newXDimension * newYDimension;//layout of bmp//file header//dib header (info header)//data//first create a new file header//everything is the same as the old bitmap except the file sizeBITMAPFILEHEADER newFH = original->fileHeader;newFH.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + newFH.bfOffBits + newDataSize;//create a new info headerBITMAPINFOHEADER newIH = original->infoHeader;newIH.biHeight = newYDimension;newIH.biWidth = newXDimension;newIH.biSizeImage = newDataSize;//allocate the data (this is only for 32 bit bitmap)DWORD* newData = (DWORD*) new DWORD[newXDimension * newYDimension];//calculate the resize ratio in x dimensionfloat ratioX = float(newXDimension) / original->infoHeader.biWidth;float ratioY = float(newYDimension) / original->infoHeader.biHeight;DWORD* newDataOffset = newData;DWORD* origDataOffset = (DWORD*) original->data;//iterate throught the original bitmap and write to the new onefor (int row = 0; row < original->infoHeader.biHeight; row++){for (int column = 0; column < original->infoHeader.biWidth; column++){int destinationPixel = int(ratioX * column - 0.5f);newDataOffset[destinationPixel] = origDataOffset[column];}newDataOffset += newXDimension;origDataOffset += original->infoHeader.biWidth;}//copy everything back to the output bitmapout->fileHeader = newFH;out->infoHeader = newIH;out->data = (unsigned char*) newData;}

sorry what's the right code tag, this one didn't indent for some reason?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement

Imagine if the entire forum contained "deleted post" in every post of every thread. Please don't remove your questions, it helps no-one and simply means people in the future are led to this thread by its title and find... nothing sad.png if you find your solution please share it for future visitors!

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

@Bacterius why isn't there a delete button for the posts? It was an obvious solution that I skimmed over (writing past array), so it wouldn't have helped anyone.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

It is a conscious decision of the forum not to have a delete button for threads. Even though you may feel there is little value sometimes it does sometimes help people. You would also be surprised at the number of people who ask a real question, get an answer and then try to remove their question and/or the whole thread. That goes very much against the spirit of a community like this and invalidates all use others have from it.

Nevertheless, if you truly feel a thread should be deleted you are free to petition the moderators to do so.

I would also like to point out that it is not a good idea to just lash out at people with the rating button. Bacterius was not the person who downvoted your post, he was just kind enough to post his reasons why you did get a minor slap on the wrist from someone else.

It is a conscious decision of the forum not to have a delete button for threads. Even though you may feel there is little value sometimes it does sometimes help people. You would also be surprised at the number of people who ask a real question, get an answer and then try to remove their question and/or the whole thread. That goes very much against the spirit of a community like this and invalidates all use others have from it.

Nevertheless, if you truly feel a thread should be deleted you are free to petition the moderators to do so.

I would also like to point out that it is not a good idea to just lash out at people with the rating button. Bacterius was not the person who downvoted your post, he was just kind enough to post his reasons why you did get a minor slap on the wrist from someone else.

well if it's a conscious decision then this is a consequence of it. My post was a long wall of code that I couldn't get formated right and at the end it was a silly mistake. So I decided to delete it. i downvoted him because it's not his place to tell me not to delete my own post when he didn't even read it to begin with.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

I actually did read what you posted, there is a "history" button you can use to see... but whatever, keep making assumptions. I'm exiting this thread now.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

I actually did read what you posted, there is a "history" button you can use to see... but whatever, keep making assumptions. I'm exiting this thread now.

oh good, then you can see why I deleted it, how many people you'd think it'd help?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Deleting threads is bad netiquette. Do kids still say that? Netiquette?
Anyway, avoid doing it. Mods will just restore the deleted post from history and in the meantime everyone will rage at the bad netiquette, and then you'll raise your hackles and everyone will have a bad time. Speaking of time, it's time to lock this shitstorm away.

This topic is closed to new replies.

Advertisement