Click here to Skip to main content
15,885,716 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

I want to execute two queries, after that, I want to return an object "article" and use it for other treatments
my function is:
C#
 Article article = new Article();
modelTest.GetQuantite(IDArt, (Qantite) =>
             {
               model.GetPrix(IDArt, Date, (res, err) =>
               {
                if (err != null)
                {
                    // TODO: NOTIFY USER
                    return;
                }
                else if (res != null)
                {
                    article.QantiteMagazin = Quantite;
                    article.Prix = res;
                    article.Total = Math.Round((Quantite.Value * res.Value), 3);
                }
              });
            });
            return article;


my issue is that when i take article.Prix or article.QuantiteMagazin; it's null
Posted

1 solution

You are trying to "break through the open door". .NET is memory managed system. You don't need to do anything special to reuse the object. In fact, it even makes no sense. As soon as you loose the access to some object (say, you assign the only reference giving access to it to null; but this is only for example; normally, you never need it), the object becomes the subject to garbage collection, which happens some time later, which not under your control.

You need to understand that references, reference-type variables or members are not the same objects as the objects they reference. You can simply assign the reference variable/member to some new object, and it will make the previously referenced object lost from your access (if you did not keep access to it through some other references). Here the mechanism is explained: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

Only you should be more careful with some objects, those which have the runtime types implementing System.IDisposable. For them, it is critically important to call System.IDisposable.Dispose before you "forget" them. Failure to do so may lead, for example, to unmanaged memory leaks, and other unpleasant consequences. In general case, this disposal is unrelated to garbage collection and managed memory, or even any other memory; it is used to ensure any kind of clean-up action. Please see:
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^],
see also: https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

Nevertheless, manages memory leaks are quite possible, but they can happen due to mistakes in code design rather than by accident. Please see my past answers:
Memory leak in WPF DataBinding[^],
Memory management in MDI forms[^],
Best way to get rid of a public static List Causing an Out of Memory[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900