Context again

Posts   
 
    
ChBaeumer
User
Posts: 175
Joined: 23-Oct-2003
# Posted on: 21-Sep-2007 09:37:55   

Hi,

is there any reason why the Context.Clear() method does not clear the _newEntities? What happens to entities which are new but should never be saved?

The problem here is the the context disappears only when the last reference to one of this new entities is garbage collected. For me this looks like a hugh memory leak.

Thank you

Christoph

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Sep-2007 12:02:16   

Context.Clear() should clear all the entities including the new entities. Which LLBLGen Pro runtime library version are you using?

ChBaeumer
User
Posts: 175
Joined: 23-Oct-2003
# Posted on: 21-Sep-2007 12:15:58   

AssemblyVersion: 2.5.0.0 AssemblyFileVersion: 2.5.07.0906

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Sep-2007 17:14:22   

It looks like a bug. The new entities ActiveContext property is not reset in this case. We will try to fix it for the next build.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 24-Sep-2007 11:15:29   

Reproduced.



[Test]
public void ContextRemoveClearTest()
{
    CustomerEntity newEntity = new CustomerEntity();
    newEntity.CustomerId = "FUBAR";
    OrderEntity newOrder = new OrderEntity();
    newOrder.OrderId = 131313;
    newEntity.Orders.Add(newOrder);
    Assert.AreEqual(newEntity, newOrder.Customers);

    CustomerEntity loadedEntity = new CustomerEntity("CHOPS");
    PrefetchPath2 path = new PrefetchPath2(EntityType.CustomerEntity);
    path.Add(CustomerEntity.PrefetchPathOrders);
    using(DataAccessAdapter adapter = new DataAccessAdapter())
    {
        Assert.IsTrue(adapter.FetchEntity(loadedEntity, path));
    }

    Context c = new Context();
    c.Add(newEntity);
    c.Add(loadedEntity);
    Assert.AreEqual(c, newEntity.ActiveContext);
    Assert.AreEqual(c, newOrder.ActiveContext);
    Assert.AreEqual(c, loadedEntity.ActiveContext);
    foreach(OrderEntity o in loadedEntity.Orders)
    {
        Assert.AreEqual(c, o.ActiveContext);
    }

    // remove them.
    c.Remove(newEntity);
    c.Remove(loadedEntity);

    Assert.IsNull(newEntity.ActiveContext); // <<<<<<<<<<<<
    Assert.IsNull(newOrder.ActiveContext);
    Assert.IsNull(loadedEntity.ActiveContext);
    foreach(OrderEntity o in loadedEntity.Orders)
    {
        Assert.IsNull(o.ActiveContext);
    }

    // clear
    c.Add(newEntity);
    c.Add(loadedEntity);
    Assert.AreEqual(c, newEntity.ActiveContext);
    Assert.AreEqual(c, newOrder.ActiveContext);
    Assert.AreEqual(c, loadedEntity.ActiveContext);
    foreach(OrderEntity o in loadedEntity.Orders)
    {
        Assert.AreEqual(c, o.ActiveContext);
    }

    c.Clear();
    Assert.IsNull(newEntity.ActiveContext);
    Assert.IsNull(newOrder.ActiveContext);
    Assert.IsNull(loadedEntity.ActiveContext);
    foreach(OrderEntity o in loadedEntity.Orders)
    {
        Assert.IsNull(o.ActiveContext);
    }
}

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 24-Sep-2007 12:15:15   

The context is actually a bit of a problem. Not the context on its own but how graph management is done with respect to the context.

What you want is: using(Context c = new Context()) { // everything done with the db here is done using c, so no more hassle with adding / removing } // c is unknown here and no object created within the using block is referring to c here.

Though that's not possible at this point. I'll file that for v3.

(edit). THe bug you reported has been fixed for the next build (09242007)

Frans Bouma | Lead developer LLBLGen Pro