If I forget to call Dispose() on an opened file stream, I know that it can cause file system errors, but will such errors ever persist AFTER my program ends? Basically, I wrote a program that was dealing with files, and I ran it and had it manage files on an external hard drive. Then, long after I had closed the program, I tried to safely remove the drive, and it said it was in use by another program, and I had closed every program I had running. Is it possible for this kind of thing to be caused by forgetting to release unmanaged resources? Or do the consequences of not releasing unmanaged resources only persist while the program is running?
The Big Programming Thread - Page 116
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
codonbyte
United States840 Posts
If I forget to call Dispose() on an opened file stream, I know that it can cause file system errors, but will such errors ever persist AFTER my program ends? Basically, I wrote a program that was dealing with files, and I ran it and had it manage files on an external hard drive. Then, long after I had closed the program, I tried to safely remove the drive, and it said it was in use by another program, and I had closed every program I had running. Is it possible for this kind of thing to be caused by forgetting to release unmanaged resources? Or do the consequences of not releasing unmanaged resources only persist while the program is running? | ||
Schnullerbacke13
Germany1199 Posts
On February 12 2012 02:29 codonbyte wrote: Hello, I have a brief question about dealing with the file system in .NET, using C#. If I forget to call Dispose() on an opened file stream, I know that it can cause file system errors, but will such errors ever persist AFTER my program ends? Basically, I wrote a program that was dealing with files, and I ran it and had it manage files on an external hard drive. Then, long after I had closed the program, I tried to safely remove the drive, and it said it was in use by another program, and I had closed every program I had running. Is it possible for this kind of thing to be caused by forgetting to release unmanaged resources? Or do the consequences of not releasing unmanaged resources only persist while the program is running? Coming from a Java background, but I am pretty sure c# will work same. 1) C# has a garbage collection, so the file ressource will be disposed when your program does not reference the C# file object anymore (Exception: you call direct windows C-API to manage the file system). However the point in time when this happens is not exactly defined, so its better to actually call Dispose() 2) When your program is closed, all locks and handles will be released, so you incident came from some explorer or something, not your program for sure | ||
codonbyte
United States840 Posts
![]() | ||
SRBNikola
Serbia191 Posts
On February 12 2012 02:56 Schnullerbacke13 wrote: Coming from a Java background, but I am pretty sure c# will work same. 1) C# has a garbage collection, so the file ressource will be disposed when your program does not reference the C# file object anymore (Exception: you call direct windows C-API to manage the file system). However the point in time when this happens is not exactly defined, so its better to actually call Dispose() 2) When your program is closed, all locks and handles will be released, so you incident came from some explorer or something, not your program for sure Garbage collector doesn't handle disposing of all, disposing resources isn't same as disposing IDisposable, Stream can stay opened and cause infinite loops, and some other bugs, however it doesn't persist after program has finished executing, but it causes trouble in run time | ||
Schnullerbacke13
Germany1199 Posts
On February 12 2012 07:01 SRBNikola wrote: Garbage collector doesn't handle disposing of all, disposing resources isn't same as disposing IDisposable, Stream can stay opened and cause infinite loops, and some other bugs, however it doesn't persist after program has finished executing, but it causes trouble in run time Ok, as told I am a Java guy .. at least in Java a System ressource associated with a class such as File is disposed once the Java Object is collected ('finalize()'). | ||
codonbyte
United States840 Posts
Thank you very much for all your advice. You rock TL.net! | ||
codonbyte
United States840 Posts
| ||
aksfjh
United States4853 Posts
| ||
Pawsom
United States928 Posts
On February 17 2012 05:55 aksfjh wrote: Not really a question, but a frustrating observation. I'm currently attemtping to teach myself C++, and it's going alright. However, when I browse problems on forums, I see a frustrating amount of, "I'm not going to help with your problem, just point out that line 7 out of 96 is bad coding practice! <insert 3 paragraph rant>." 75% of the time, they don't even tell you that their useless advice won't help you solve the problem. Ugh! This is probably because most if not all of your problems can be solved with proper use of google. | ||
mmp
United States2130 Posts
On February 17 2012 05:55 aksfjh wrote: Not really a question, but a frustrating observation. I'm currently attemtping to teach myself C++, and it's going alright. However, when I browse problems on forums, I see a frustrating amount of, "I'm not going to help with your problem, just point out that line 7 out of 96 is bad coding practice! <insert 3 paragraph rant>." 75% of the time, they don't even tell you that their useless advice won't help you solve the problem. Ugh! Your problems are boring. From the point of view of someone who knows the answer but isn't responding, a newbie is born every minute, so why should they waste their time doing your thinking for you? Read the fucking manual. They also might not know the answer (most forums have a lot of bad answers), but want to say something anyway. | ||
Clearout
Norway1060 Posts
Error: Could not find or load main class ovinger.Oving4Oppg2 When I google around, all I find is the same about checking my classpath, but it has the same path as all the others in the project and they work just fine. related code: + Show Spoiler + public class Oving4Oppg2 { picture of run configurations (the same for all classes in project): + Show Spoiler + ![]() Any suggestions? Thanks. Edit: Found out that the .class file does not get made in the projects bin folder. Edit2: Found some strange shit in my run configurations and fixed that, now it works. | ||
aksfjh
United States4853 Posts
On February 17 2012 10:31 mmp wrote: Your problems are boring. From the point of view of someone who knows the answer but isn't responding, a newbie is born every minute, so why should they waste their time doing your thinking for you? Read the fucking manual. They also might not know the answer (most forums have a lot of bad answers), but want to say something anyway. It's not necessarily my problem anyways. I'm not the one posting. Anyways, here's a good example: Creating a program that requires a "while" or "do while" loop to gather a set of numbers. There is a number terminator (like -1). However, when you input a character or string instead of an int with the "cin" function, it spits out the previous prompt in a rapid infinite loop, which must be aborted. The actual solution to this problem relies on some very unintuitive programming, but without somebody teaching you, you'll try exception handling, "if" statements, etc. When looking for an answer to this issue, the #1 response was the improper use of "throw" from the submitted program. However, when he fixed that "issue," he still had the same cin problem as before. | ||
BottleAbuser
Korea (South)1888 Posts
So far, I've been identifying blocks of code that are similar, generalizing them into library functions, and including a lib.js in my common header. Of course, this means we're loading code that's unneeded for a sizable chunk of the pages on the site. I'm starting to wonder if there are better ways such as dynamically calling javascript resources (maybe with ajax). Is this a good idea? I'm thinking I'd need to eval() the returned script, but that seems like a big no-no. | ||
Kentor
![]()
United States5784 Posts
On February 17 2012 22:58 BottleAbuser wrote: I've been doing this project that's supposed to be all flashy so I'm using a lot of javascript (jQuery). I've been having trouble keeping my code clean. Any resources that might help? So far, I've been identifying blocks of code that are similar, generalizing them into library functions, and including a lib.js in my common header. Of course, this means we're loading code that's unneeded for a sizable chunk of the pages on the site. I'm starting to wonder if there are better ways such as dynamically calling javascript resources (maybe with ajax). Is this a good idea? I'm thinking I'd need to eval() the returned script, but that seems like a big no-no. What are you using on the server side? Why not just include the minimum javascript files needed for general pages and add on more specific ones to specific pages? | ||
King K. Rool
Canada4408 Posts
Webpage seems to load like 10x slower, not sure if something's configured wrong or MS Azure cloud just slow as fuck. | ||
mmp
United States2130 Posts
On February 17 2012 22:58 BottleAbuser wrote: I've been doing this project that's supposed to be all flashy so I'm using a lot of javascript (jQuery). I've been having trouble keeping my code clean. Any resources that might help? So far, I've been identifying blocks of code that are similar, generalizing them into library functions, and including a lib.js in my common header. Of course, this means we're loading code that's unneeded for a sizable chunk of the pages on the site. I'm starting to wonder if there are better ways such as dynamically calling javascript resources (maybe with ajax). Is this a good idea? I'm thinking I'd need to eval() the returned script, but that seems like a big no-no. (1) How large is your lib.js? It's probably trivially small. (2) Do not use eval. Do what jQuery does, append a <script> element into the document. Post a link to your source for detailed comments. | ||
mmp
United States2130 Posts
On February 17 2012 19:04 aksfjh wrote: It's not necessarily my problem anyways. I'm not the one posting. Anyways, here's a good example: Creating a program that requires a "while" or "do while" loop to gather a set of numbers. There is a number terminator (like -1). However, when you input a character or string instead of an int with the "cin" function, it spits out the previous prompt in a rapid infinite loop, which must be aborted. The actual solution to this problem relies on some very unintuitive programming, but without somebody teaching you, you'll try exception handling, "if" statements, etc. When looking for an answer to this issue, the #1 response was the improper use of "throw" from the submitted program. However, when he fixed that "issue," he still had the same cin problem as before. Yeah that sounds terribly boring. | ||
tec27
United States3696 Posts
On February 18 2012 13:33 mmp wrote: (1) How large is your lib.js? It's probably trivially small. (2) Do not use eval. Do what jQuery does, append a <script> element into the document. Post a link to your source for detailed comments. (2) is pretty bad advice, considering jQuery uses eval() on scripts you try to insert into the DOM through it. There are things you don't want to eval, such as data that comes from domains you do not control, but people need to stop being so irrationally fearful of it. It has valid uses. To respond to BottleAbuser, the size of your javascript probably doesn't mean a whole lot. Round trips are usually going to be far more expensive, and browsers have limits on the number of concurrent downloads from a site so you want to keep that number small. The general recommendation is to keep one large script file and make sure its minified, and that it has good cache settings (IE: cache for a really long time < 1 year). If you truly need very little of the javascript on almost all of the pages, I would look for a way to not include that script file from the server. Asyncronous module loading is just generally never a good choice. | ||
BottleAbuser
Korea (South)1888 Posts
For example, I have a page that serves as a drag & drop HTML editor. It takes a bunch of content from the DB and loads it into hidden divs, with a list of titles representing them, which are then made draggable. I have a few templates which have droppable zones, and are loaded into the editor area upon menu clicks. Saving edits is done with ajax. With visual effects and all, this amounts to about 70 lines of code on the $.onload() function, each initializing something different, and something like 15 different function declarations that each do their own thing (showBusy(), hideBusy(), save(), initDroppable(), ...). Right now, I can make sense of it but the next person who comes along, or me in a few months, will have a bitch of a time navigating this whole "everything is in this one file here, hope you can read my English comments" deal. I guess what I'm looking for is some way of logically dividing this stuff up so it's easier to look at. I don't have a problem with compiling everything into one file for production use, but I'd really like to have readable source. (Also, I'm sure there has got to be a better way of displaying javascript code than what Eclipse does. I end up with lines that are rarely over 40 characters long, and so I end up with this column of code that uses 1/5 of what my widescreen offers and forces me to scroll like a madman when I'm looking for that one piece of code that does this or that.) | ||
codonbyte
United States840 Posts
On February 17 2012 05:55 aksfjh wrote: Not really a question, but a frustrating observation. I'm currently attemtping to teach myself C++, and it's going alright. However, when I browse problems on forums, I see a frustrating amount of, "I'm not going to help with your problem, just point out that line 7 out of 96 is bad coding practice! <insert 3 paragraph rant>." 75% of the time, they don't even tell you that their useless advice won't help you solve the problem. Ugh! You're trying to teach yourself C++? That's awesome, man! I taught myself C++, and it wasn't easy. What reference are you using? I used Sams Teach Yourself C++ in 24 Hours, and in retrospect, I don't think it was the best resource out there. There are a LOT of bad programming books out there. Anyway, your problem sounds really frustrating. I think at least sometimes people just quickly skim the code and think that their unhelpful advice may actually solve your problem. This happens to me all the time when I'm debugging my own code. Debugging logic errors is more frustrating than mutalisks killing all your add-ons. Just out of curiosity, what is it you wish to do with programming? | ||
| ||