Link to home
Start Free TrialLog in
Avatar of newyuppie
newyuppieFlag for Ecuador

asked on

attempted to read/write protected memory... THREADING probably

hi
i have a .net 2, visual basic app im developing, and im trying to make it single instance via the apps config menu. i also placed a msgbox in the StartNextInstance event of the application just to see if it gets there.
the thing is, it shows me the form, and after a second or 2, i get an accessviolationerror, "attempted to read/write protected memory...".
i used to get this a while ago when i was programming internet access for my app, if i had Eset's NOD32 antivirus engine active i got that message, otherwise i didnt.
so i feel it has something to do with the threading or something like that. maybe its trying to access something that its already accesed by another process? did anybody face this problem before? how do i go about debugging and solving this? i really need this app to be single instance, and in the event of a second instance, pass the command line args to the first instance and then kill the second instance.
please help?
thanks
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of newyuppie

ASKER

hi Idle_Mind
thanks for responding so fast

ive been debugging line by line, and exactly on this argument

For Each arg In e.CommandLine

i get this error:
System.AccessViolationException was unhandled
  Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
  Source="System"
  StackTrace:
       at System.Net.UnsafeNclNativeMethods.OSSOCK.WSAGetOverlappedResult(SafeCloseSocket socketHandle, IntPtr overlapped, UInt32& bytesTransferred, Boolean wait, IntPtr ignored)
       at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
       at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

what could be wrong?
thank you
I don't know if I am I understand the question well but if you want to ensure only one instance of your app is running then here is a couple of ways to do it

http://www.ai.uga.edu/mc/SingleInstance.html
Try creating a New, blank project with just the instancing code and see if it still crashes...
idlemind, i have the same issue...
I have no idea then...

Do you have all the latest updates/service packs for your system?
have win xp pro sp2
.net framework 2.0.50727
visual basic 2005 8.0.50727.42 (RTM.50727-4200)

.. i really dont understand this. one more thing is very odd, when i debug step by step (F8) i get the error on that line
For Each arg In e.CommandLine

i have tested using the immediate window, i did "?e.commandline" when the cursor was stopped just before running that line, and i get the print of the commandline with no error, everything ok. but if i run that line i get the error......
You could try reinstalling VB...   =\
i did last night, i guess ill have to format disk? if you think of soemthing else please let me know ?

to qbzu, thanks for the link. ill check it out, it's my second option (using mutex), altough its in C, and im using visual basic. do you have a link like that to a VB code?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Right gbzhhu...there are many different ways to figure out if an application is already running.

The tricky part is getting the command line parameters for the new instance, passed back to the already running instance.  Something which your code does not address at all.

The .Net 2.0 Application Model is ~SUPPOSED~ to handle this gracefully:
http://msdn2.microsoft.com/en-us/library/w3xx6ewx.aspx

It does in fact work, and I have personally written code before that does just that.

We just need to figure out why newyuppies computer is crashing...  =)
Idle_Mind,

I don't know much about .NET 2.0 (just reading a book on it).  Thanks for the link, I'll have a read.  Though I am not the asker I am here to learn from others and contribute when I can

Does newyuppies code work on another PC?  I'd suggest doing that first
thanks for the contribution gbzhhu. idle_mind is right, what i need is to pass the arguments over to the first instantiated app. i've been reading some codes and am trying to adapt the mutex/hwnd duo instead of visual basic 2005 native "make single instance".
please if you have any more ideas let me know.

ps. it does work on another computer (the runtimes), but i dont have the developing program on the other comp so its a pain to install my app on the other comp every time i want to test it.

thanks for all the contributions, ill keep this open for a while to see, otherwise both will get the points for helping me. thanks
I will close this question and give IdleMind and gbzhhu the points (proportionally) for participating and trying to help. I think its a problem with my installation of Visual Studio so i should format C and reinstall to really see if that helps. anyway, thats not gonna happen anytime soon (its a pain in the ars#e).

i ended up using a mutex to check for single instance, and found a code somewhere to use hWnd messages to pass information to the owner app.

ps. idlemind, you mentioned that you have written code that performs this, maybe you could send me an example of how you did it, that would be very helpful to me as i dont particularly like the hWnd method, it makes all my debugging a pain because everytime i keep debugging the hWnd messages for hours...
The example is posted at the top (it passes the command line args for multiple instances to the first instance)...

I was just stating that I have personally tested the code and had it work "as advertised" by Microsoft.

Here is an app that uses the new application model to make a tray icon pop up:
https://www.experts-exchange.com/questions/21767186/Open-tray-form-when-user-double-clicks-shortcut.html