.net - Creating a memory leak in C# or VB.Net -
inspired this question, wondering possible ways create memory leak in .net. once found 1 odbc data access. has had experiences latest version?
edit: there seemingly normal, harmless uses can cause memory leaks?
the easiest way create memory leak misuse facilities designed interop, since deal unmanaged memory.
for example, allocate gchandle
pointing object, , never free it.
edit: there seemingly normal, harmless uses can cause memory leaks?
only 1 know of, particularly in ui programs. possible in winforms, became common due wpf mvvm:
the key point many people overlook delegate contains reference object on runs.
so, if 1 using pattern such mvvm two-way bindings (implemented using events, comprised of delegates), , if view changed different view same viewmodel, default viewmodel's updates remain bound both views, causes leak of old view.
this can in theory happen delegate, in practice it's not common because subscriber outlives publisher or unsubscribes.
a similar situation exists when lambda event handlers unsubscribed:
timer.elapsed += (_, __) => myobj.notify(); timer.elapsed -= (_, __) => myobj.notify();
in case, though lambda expressions identical, represent different handlers, elapsed
event still invoke notify
. second line above has no effect; doesn't unsubscribe, nor raise error.
note improper unsubscriptions caught during testing, seldomly cause "leaks" in released code. in contrast, mvvm situation above causes no observable side effects (other memory , resource leak).
Comments
Post a Comment