# Loading an Edit from a file and assigning it

**URL:** <https://forum.juce.com/t/loading-an-edit-from-a-file-and-assigning-it/64972>\
**Category:** Tracktion Engine\
**Created:** [January 21, 2025, 10:38pm UTC](https://forum.juce.com/t/loading-an-edit-from-a-file-and-assigning-it/64972 "2025-01-21T22:38:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesc0](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@Jesc0](https://forum.juce.com/u/Jesc0)\
**Post date:** [January 21, 2025, 10:38pm UTC](https://forum.juce.com/t/loading-an-edit-from-a-file-and-assigning-it/64972/1 "2025-01-21T22:38:16Z")

</div>

I feel like what I’m trying to do is simple enough

```auto
edit = te::loadEditFromFile(engine, fileChosen);

```

The Edit is loaded in just fine, but VS hits a breakpoint at the EditItemCache destructor with the following assertion

```auto
jassert (knownEditItems.empty()); 

```

Looking at knownEditItems, it holds 4 items which seem to point to the global tracks. I’m really not sure why those are still there in cache.

I’ve been trying to find a work around to no avail and I’m kinda hoping there’s a simple solution staring me in the face to which I’m blind to. I would appreciate any help.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [January 21, 2025, 11:38pm UTC](https://forum.juce.com/t/loading-an-edit-from-a-file-and-assigning-it/64972/2 "2025-01-21T23:38:25Z")

</div>

Can you share some simple example code that triggers that assertion please?

---

<div class="post-metadata">

**Author:** ![Jesc0](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@Jesc0](https://forum.juce.com/u/Jesc0)\
**Post date:** [January 22, 2025, 1:22am UTC](https://forum.juce.com/t/loading-an-edit-from-a-file-and-assigning-it/64972/3 "2025-01-22T01:22:05Z")

</div>

My bad, should’ve went into a little more detail.  
I made my MainComponent inherit juce::MenuBarModel methods so I can go to File-\>Open to get the Edit file while my application is running.

```auto
// Menu Bar Actions
void MainComponent::menuItemSelected(int menuItemID, int topLevelMenuIndex)
{
	// Handle menu selections
    switch (menuItemID)
    {
	case 1: // Open
    {
        // Choose an Edit file
        FileChooser chooser("Select edit file", juce::File{}, "*");
        if (chooser.browseForFileToOpen())
        {
            File fileChosen = chooser.getResult(); // Get the file
            edit = te::loadEditFromFile(engine, fileChosen); 
        }
        break;
    }
	case 2: // Save
    {
        FileChooser chooser("Select the file to save to", juce::File{}, "*");
        if (chooser.browseForFileToSave(true))
        {
            File fileChosen = chooser.getResult(); // Get the file chosen
            editFileOps.saveAs(fileChosen, true); // Save the file there
        }
        break; // Save the current edit
    }
	case 3: juce::JUCEApplication::quit(); break; // Exit
	…
	}
}

```

Another thing my MainComponent does is pass the edit to its subcomponents

```auto
   ...
   std::unique_ptr <tracktion_engine::Edit> edit = te::createEmptyEdit(engine, {});
   tracktion_engine::SelectionManager selectionManager{ engine };
   tracktion_engine::EditFileOperations editFileOps{ *edit};

   //Visuals
   EditVisual editVisual{ *edit, selectionManager };

   // Components (GUI Components)
   MenuBarComponent menuBar{ this };
   ArrangementModeComponent ArrangementMode{*edit, selectionManager };
   ...

```

All the subcomponents have an Edit reference member so they can access it. I suspected this is where my problem arises when I try to change the edit but I’m unsure exactly how or where.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [January 22, 2025, 9:58am UTC](https://forum.juce.com/t/loading-an-edit-from-a-file-and-assigning-it/64972/4 "2025-01-22T09:58:31Z")

</div>

You can’t take a reference to a new, empty Edit and then overwrite that when you open a new Edit. All the other objects will now be referring to a dangling reference which will cause crashes and unpredictable behaviour.

You probably want to wrap all of those objects in to something that takes an Edit and recreate them all when you load a new Edit.

---

<div class="post-metadata">

**Author:** ![Jesc0](https://avatars.discourse-cdn.com/v4/letter/j/919ad9/32.png) [@Jesc0](https://forum.juce.com/u/Jesc0)\
**Post date:** [January 22, 2025, 7:39pm UTC](https://forum.juce.com/t/loading-an-edit-from-a-file-and-assigning-it/64972/5 "2025-01-22T19:39:29Z")

</div>

Thank you, Dave!
