Access violation on closing Window when using Viewport


When I use a viewport instead of adding my component directly, I get an access violation when I close the Window:

Demo.exe: 0xC0000005: Access violation reading location 0xDDDDDDDD

Here is my code:

class ProjectBrowserTabComponent : public TabbedComponent
	void initTabBarNotifyComponent();

	ProjectEntryListComponent m_remote_proj;
	ScopedPointer<ProjectEntryListComponent> m_local_proj = new ProjectEntryListComponent();

	ProjectEntryListController *pelcont;
	ProjectEntryListController *pelccont2;

	ScopedPointer<Viewport> vp = new Viewport;

ProjectBrowserTabComponent::ProjectBrowserTabComponent() : TabbedComponent(TabbedButtonBar::TabsAtTop) {
	vp->setSize(100, 200);
	vp->setViewedComponent(m_local_proj.get(), true);
	addTab("Remote projects", Colours::lightgrey, &m_remote_proj, true);
	addTab("Local projects", Colours::lightgrey, vp, false);
	addTab("Shared projects", Colours::lightgrey, new ProjectEntryListComponent(), false);
	setSize(getLocalBounds().getWidth(), getLocalBounds().getHeight());

What am I doing wrong?

Size of GUI component and viewport use with relative widths?

When you call:

vp->setViewedComponent(m_local_proj.get(), true);

you’re telling the viewport to take ownership of the component and delete it when it’s no longer needed but your m_local_proj is a ScopedPointer which will delete the object that it refers to when it goes out of scope, so you end up with a double deletion. Either use ScopedPointer::release() instead of ScopedPointer::get() or set the deleteComponentWhenNoLongerNeeded boolean to false.


Also you’ll want to change the false to true in this line:

addTab("Shared projects", Colours::lightgrey, new ProjectEntryListComponent(), false);


Thanks a lot it solved my issue