So I am having trouble with the most popular error ever, memory access violation. To make a long story short I am dabbling with an old program I once wrote, it's main purpose is to parse some stuff and download some files. (It's too huge all in all so I will only post relevant snippets)

The access violation is odd because it occurs after a while (to be exact, the 8th time the following snippet is called) which makes it even harder to figure out where the problem is. The error occurs at this line of code:

URLDownloadToFileA(0, Download, "somefile.txt", 0, 0);

Download is a char array initialized like this

char Download[200] = {0};

It is only touched in code at two locations, first one is when I fill it with data (I actually construct a url to pass to URLDownloadToFileA):

for(int i = 0; i < SomeValue; i++)
Download[i] = SomeContent[i]

Something like that. Naturally SomeValue will never be bigger than the array size.

Second time is when I clear it, I do this after downloading a file before I construct the next url to pass

for(int i = 0; i < 200; i++)
Download[i] = '\0';

That's it. I'm puzzled, maybe I made a huge mistake somehow but I don't see how this could lead to memory access violations. And if I had screwed up somehow I would assume I would get a error directly and not after running the program for a while.

Debugger doesn't give anymore info than that the URLDownloadToFileA is the faulty line. So I guess the error must occur when it tries to access Download. I tried everything but I'm stuck with stupid dev cpp and it doesn't seem to be any way to find out more.

I know this is a little vague and all but perhaps someone have an idea on why this keep happening?

Recommended Answers

All 2 Replies

Naturally SomeValue will never be bigger than the array size.

Many a program have been buried for just that belief.
Check that value.
In addition, it might be enlightening to see your URLDownloadToFileA implementation to ensure that the problem isn't within that method.

You are right. I also check that, I just didn't bother to post that snippet here. Anyway if this would be the problem (somehow), the access violation should have been raised while filling the array right (since it would owerflow)?

Implementation looks like this:

typedef int * (*URLDownloadToFileA)(void*,char*,char*,DWORD,void*);
HINSTANCE LibHnd = LoadLibrary("Urlmon.dll");
URLDownloadToFileA URLDownloadToFile =  (URLDownloadToFileA) GetProcAddress(LibHnd,"URLDownloadToFileA");

I also tried another approach to solve this problem. I simply put together the url by appending strings and then passed UrlDownload() a cstyle string instead (and changed the type for the url to const char *). Unfortunately this got even messier. Although I carefully cleared the strings I would get occasional memory access violations while appending them and I would get the same access violation in UrlDownload() anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.