Hi,
I appreciate any help with this issue.
I have an application which runs fine both in debug and release mode, if I launch it within the IDE. The debug compiled .exe "/MDd" can also run fine outside the IDE. However, the release compiled .exe "/MD" gives a run time error:
************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at free(Void* )
I have triple checked all memory allocations and frees and all seems correct to me (I am an experienced C++ developer). Of course, it wouldn't be the first time I missed something.
I just can't make sense of why it runs in the IDE and not outside.
I find that if I comment out my frees/deletes (I've tried both ways, ie malloc/free and new/delete) it does not give this error. Ie, comment out:
if
(xData != NULL) { free(xData); xData = NULL; }Must I use gcnew or something This is happening inside of a managed class.
Any pointers would be appreciated.
Thanks,
W

Runtime Error On Delete / Free, only with "/MD" and outside the IDE
Arthg2000
Great!
Once you have the smaller repro, please post it straight ahead.
Also, you might want to check out the following past threads in case they help:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=6344&SiteID=1 http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=88234&SiteID=1 Thanks, Ayman Shoukry VC++ TeamRatzu
Yes, I have a few double and int pointers which are unmanaged inside the managed class. I've understood this to be okay, I've done it many times in 2K2/2K3. Perhaps this has changed in 2K5
Thanks again for your help.
phita
vcooking
Thanks again. I checked out those links, they don't really apply to my problem, imho.
I've tried all of the following now:
- Removed all instances of NULL and replaced with nullptr (even though I read NULL and nullptr are compatible).
- Pinned all pointers to any dynamically allocated native types inside my managed class, so even if the garbage collector would move stuff around, these pointers would remain pointed where I wanted.
Still, no luck.
I really, really would like to understand how running in release mode in the IDE is different than running outside the IDE, with the exception of the debugger being attached.
Is there an IDE setting I can make which would cause my application to run the same way as it does outside the IDE
I'm starting to think I need to write this application in 2K3, which is a shame because I really like the new IDE as well as the new controls, particluary the toolstrips. I can't think of anything else that could be wrong at this point and stripping down the application is going to be harder than I thought (ie, take more time that I don't have now).
If I can just understand how to get the IDE to execute the application the same way it would otherwise run, I'd be fine. I've even turned on breakpoints on every thrown exception and it never breaks in the IDE. Then presto, run it outside the IDE and things go all bad.
Anything you can think of would be a help. Thanks for the links and the help.
Hooray_for_Boobies
I suppose I can also word my question differently. Is there a way to cause the IDE to execute the application the same way as it would otherwise execute outside the debugger. No matter what I try, I can not reproduce the issue using the debugger (release or debug compile). If I can just get some visbility into the error I may catch something bad that I did. I even tried Windbg, no luck.
I believe I understand the difference between debug and release compiles in the sense of memory padding/guarding, etc. What I don't understand is what else is different about a release compiled application running in the IDE, except that a debugger is attached to the process.
Thanks for any help. This is killing me!
W
blade00007
Hi Ayman. Thanks for the reply. Actually, the code snippet above is what seems to cause the issue. I can't really put down the whole function here, however I can clarify the following:
Basically, I have a double *xData, which I initialize to NULL. Each time I get into this function call, I check if it is NULL, and if not, I free up the pointer and then allocate the new memory block for xData. When I'm done with it, I again free it and set it to NULL. This is way standard code for me and has never given me an issue VC6.0 through 2003, or in any other IDE/OS for that matter.
Here is some code:
A: --> if (xData != NULL) { free(xData); xData = NULL; }
B: --> xData = (double *) malloc(sizeof(double)*xDistancePx);
I check that xDistancePx is not zero above this code.
If I comment out the line A above, it does not crash, but the application also doesn't close properly. I then need to kill the process in Task Manager. Maybe this gives another hint.
I'm really confused.
Thanks again for the reply. If I can boil it down to simple code that reproduces the issue, I will certainly post it.
N S S
Whe you are running through the IDE, there are lots of dlls loaded (not just the debugger) and hence memory layout changes which might affect the outcome specially if you have memory issues in your application.
Did you try a sample to reproduce the issue May be this can tell you what is going wrong in the application.
Thanks, Ayman Shoukry VC++ Teamsaravanan_first
Thanks Ayman.
I'm experienced enough to know when I've hit a bug lower down in the overall system than I can explain. I have uncovered a few more details, proably won't help to put them here. It is related to EnterCriticalSection, which I don't call myself. I have now combed through EVERY line of my code and stripped off what I could. No luck.
Is there any "paid" MS support mode for VC++ Express At this point, I would pay for some help (not to imply that I don't appreciate your help).
Thanks,
Warren
Jason Maronge
Here is a link to the prodcut support service: http://support.microsoft.com/select/ LN=en-us&target=assistance&x=7&y=11
Nevertheless, if you manged to put a sample that I can look into, I will be more than happy to do that.
Please let me know how it goes and if I can help in any way.
Thanks, Ayman Shoukry VC++ TeamShy Cohen - MSFT
Could you post a small sample reproducing the issue so that folks on the forums can help
Thanks, Ayman Shoukry VC++ TeamNathanLiu
Do you have native members inside your managed class
Thanks, Ayman Shoukry VC++ TeamErik Arfeuille
I tried a simple case on my machine and it works as expected both with & without the IDE.
I need more details to be able to reproduce the issue. Is this a native or managed application and how is the managed class related to the sample you posted I am just trying to reproduce the issue so that I can help.
Thanks, Ayman Shoukry VC++ Teammrfantasy999
Hi Ayman,
Free "frees" up memory allocated dynamically. Same as delete. You use free with malloc, delete with new.
I have tried both new/delete and malloc/free just to see. Both give the same result.
Another strange thing I wonder is, could the bug be related to GDI This particular managed class I've developed is using the GDI (it is a plot library). I notice that when this happens, the Windows Forms panel I "draw" to gets a giant Red 'X' and border on it. I wonder if this gives me some clue; because I certainly don't draw this.
I appreciate your response.
Thanks,
W
SQLMonger66
Thanks for your help.
The application is managed and contains the managed class in question. I'm trying to develop a simpler stripped down version which reproduces the issue.