Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / WSABUF confusion

Author
Message
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 3rd Jul 2008 20:04 Edited at: 3rd Jul 2008 20:09
I'm trying to create a function that will convert a string and store it in a WSABUF structure but I'm having trouble doing so. My failings are probably due to using pointers wrongly or not fully understanding the way in which WSABUF structures work.

In the code below "stringtowsa" is supposed to take parameter one (a string) and store it in parameter two (a pointer to a WSABUF structure). It does this, but when cTemp is deleted so is the buffer.

I think that the problem is with "Return->buf = cTemp;" as this may be setting the buf pointer to point to cTemp rather than setting it to equal what is in cTemp. If this is the case, how do I set it to equal what is in cTemp? I tried but kept getting errors.

This brings me onto my next question; why do I need to set the WSABUF to equal a character array before loading it with data from the string in order to avoid an error?

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 3rd Jul 2008 20:12 Edited at: 3rd Jul 2008 20:15
Why not assign Return->buf directly rather than using cTemp

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 3rd Jul 2008 20:15
I forgot to mention why I'm copying each character individually rather than doing it all at once using the = sign. I found that only the first character is copied (not sure why) in the below code. Comparing in debug mode I see 8 bytes for the string and only 1 for the WSABUF:
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 3rd Jul 2008 20:19
Quote: "Why not assign Return->buf directly rather than using cTemp"


If I remove the cTemp code and assign directly I get access violations (I don't know why):
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Jul 2008 20:19 Edited at: 3rd Jul 2008 20:20
All you're doing is pointing buf to the same place as cTemp, so when you free that memory you can't use it anymore. Basically, don't free the memory at the end of the function. 'buf' is simply a pointer, you have to allocate memory for it yourself.

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 3rd Jul 2008 20:29
But if he assigns the memory using new he has to free it before exiting the function, otherwise the next time the function is called it will reallocate new space and create a memory leak.

As you said, you have to allocate the buffer space statically and pass the address of that buffer to Return->buf. That pretty much means either a global declaration or the address needs to be passed up the function chain.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 3rd Jul 2008 20:38 Edited at: 3rd Jul 2008 20:41
Quote: "buf' is simply a pointer, you have to allocate memory for it yourself."


I came to that conclusion too but just to be sure I tested it.

In the below code:
Surely both WSABUF should be equal to the Chars variable since they were allocated the same memory; both variables appear to have different memory unlinked to Chars.



[edit]
Without these two lines:
Buf1.buf = "Buf1";
Buf2.buf = "Buf2";

they are linked with Chars but with them they are not, so this means that it is possible to get the WSABUF structure to allocate its own memory?
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Jul 2008 20:44 Edited at: 3rd Jul 2008 20:53
Quote: " Buf1.buf = Chars;
Buf2.buf = Chars;

Buf1.buf = "Buf1";
Buf2.buf = "Buf2";
"

You do realise you're just assigning both buffers twice? First you're pointing them to 'Chars', then you're pointing them to two different string constants.

It's not possible to get the WSABUF structure to allocate its own memory, no. You could write your own class that automatically handles memory allocation/deletion though.

Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 3rd Jul 2008 21:07
Quote: "You do realise you're just assigning both buffers twice?"

Oh.. ( *hits self*), I should probably goto sleep before I post anything else stupid.. Thanks for your help guys!
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Jul 2008 21:47
Yes you should. No problem.

Login to post a reply

Server time is: 2024-11-17 19:14:27
Your offset time is: 2024-11-17 19:14:27