Skip to content

Commit fe5ab9a

Browse files
committed
Remove the naked new in allocation of AudacityProject
1 parent 3d84c0d commit fe5ab9a

11 files changed

Lines changed: 100 additions & 56 deletions

File tree

‎src/AudacityApp.cpp‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ void QuitAudacity(bool bForce)
275275
// BG: Are there any projects open?
276276
//- if (!gAudacityProjects.IsEmpty())
277277
/*start+*/
278-
if (gAudacityProjects.IsEmpty())
278+
if (gAudacityProjects.empty())
279279
{
280280
#ifdef __WXMAC__
281281
AudacityProject::DeleteClipboard();
@@ -285,8 +285,10 @@ void QuitAudacity(bool bForce)
285285
/*end+*/
286286
{
287287
SaveWindowSize();
288-
while (gAudacityProjects.Count())
288+
while (gAudacityProjects.size())
289289
{
290+
// Closing the project has global side-effect
291+
// of deletion from gAudacityProjects
290292
if (bForce)
291293
{
292294
gAudacityProjects[0]->Close(true);
@@ -339,12 +341,12 @@ void SaveWindowSize()
339341
bool validWindowForSaveWindowSize = FALSE;
340342
AudacityProject * validProject = NULL;
341343
bool foundIconizedProject = FALSE;
342-
size_t numProjects = gAudacityProjects.Count();
344+
size_t numProjects = gAudacityProjects.size();
343345
for (size_t i = 0; i < numProjects; i++)
344346
{
345347
if (!gAudacityProjects[i]->IsIconized()) {
346348
validWindowForSaveWindowSize = TRUE;
347-
validProject = gAudacityProjects[i];
349+
validProject = gAudacityProjects[i].get();
348350
i = numProjects;
349351
}
350352
else
@@ -370,7 +372,7 @@ void SaveWindowSize()
370372
else
371373
{
372374
if (foundIconizedProject) {
373-
validProject = gAudacityProjects[0];
375+
validProject = gAudacityProjects[0].get();
374376
bool wndMaximized = validProject->IsMaximized();
375377
wxRect normalRect = validProject->GetNormalizedWindowState();
376378
// store only the normal rectangle because the itemized rectangle
@@ -502,7 +504,7 @@ static gboolean save_yourself_cb(GnomeClient *client,
502504
return TRUE;
503505
}
504506

505-
if (gAudacityProjects.IsEmpty()) {
507+
if (gAudacityProjects.empty()) {
506508
return TRUE;
507509
}
508510

@@ -723,7 +725,7 @@ void AudacityApp::MacNewFile()
723725
// This method should only be used on the Mac platform
724726
// when no project windows are open.
725727

726-
if (gAudacityProjects.GetCount() == 0) {
728+
if (gAudacityProjects.size() == 0) {
727729
CreateNewAudacityProject();
728730
}
729731
}
@@ -886,19 +888,19 @@ void AudacityApp::OnTimer(wxTimerEvent& WXUNUSED(event))
886888
if (ShouldShowMissingAliasedFileWarning()) {
887889
// find which project owns the blockfile
888890
// note: there may be more than 1, but just go with the first one.
889-
size_t numProjects = gAudacityProjects.Count();
891+
size_t numProjects = gAudacityProjects.size();
890892
wxString missingFileName;
891893
AudacityProject *offendingProject = NULL;
892894

893895
m_LastMissingBlockFileLock.Lock();
894896
if (numProjects == 1) {
895897
// if there is only one project open, no need to search
896-
offendingProject = gAudacityProjects[0];
898+
offendingProject = gAudacityProjects[0].get();
897899
} else if (numProjects > 1) {
898900
for (size_t i = 0; i < numProjects; i++) {
899901
// search each project for the blockfile
900902
if (gAudacityProjects[i]->GetDirManager()->ContainsBlockFile(m_LastMissingBlockFile)) {
901-
offendingProject = gAudacityProjects[i];
903+
offendingProject = gAudacityProjects[i].get();
902904
break;
903905
}
904906
}
@@ -1973,8 +1975,10 @@ void AudacityApp::OnEndSession(wxCloseEvent & event)
19731975

19741976
// Try to close each open window. If the user hits Cancel
19751977
// in a Save Changes dialog, don't continue.
1976-
if (!gAudacityProjects.IsEmpty()) {
1977-
while (gAudacityProjects.Count()) {
1978+
if (!gAudacityProjects.empty()) {
1979+
while (gAudacityProjects.size()) {
1980+
// Closing the project has side-effect of
1981+
// deletion from gAudacityProjects
19781982
if (force) {
19791983
gAudacityProjects[0]->Close(true);
19801984
}
@@ -2077,7 +2081,7 @@ void AudacityApp::OnMenuNew(wxCommandEvent & event)
20772081
// this happens, and enable the same code to be present on
20782082
// all platforms.
20792083

2080-
if(gAudacityProjects.GetCount() == 0)
2084+
if(gAudacityProjects.size() == 0)
20812085
CreateNewAudacityProject();
20822086
else
20832087
event.Skip();
@@ -2093,7 +2097,7 @@ void AudacityApp::OnMenuOpen(wxCommandEvent & event)
20932097
// all platforms.
20942098

20952099

2096-
if(gAudacityProjects.GetCount() == 0)
2100+
if(gAudacityProjects.size() == 0)
20972101
AudacityProject::OpenFiles(NULL);
20982102
else
20992103
event.Skip();
@@ -2109,7 +2113,7 @@ void AudacityApp::OnMenuPreferences(wxCommandEvent & event)
21092113
// this happens, and enable the same code to be present on
21102114
// all platforms.
21112115

2112-
if(gAudacityProjects.GetCount() == 0) {
2116+
if(gAudacityProjects.size() == 0) {
21132117
GlobalPrefsDialog dialog(NULL /* parent */ );
21142118
dialog.ShowModal();
21152119
}
@@ -2127,12 +2131,12 @@ void AudacityApp::OnMenuExit(wxCommandEvent & event)
21272131
// all platforms.
21282132

21292133
// LL: Removed "if" to allow closing based on final project count.
2130-
// if(gAudacityProjects.GetCount() == 0)
2134+
// if(gAudacityProjects.size() == 0)
21312135
QuitAudacity();
21322136

21332137
// LL: Veto quit if projects are still open. This can happen
21342138
// if the user selected Cancel in a Save dialog.
2135-
event.Skip(gAudacityProjects.GetCount() == 0);
2139+
event.Skip(gAudacityProjects.size() == 0);
21362140

21372141
}
21382142

‎src/DeviceManager.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ void DeviceManager::Rescan()
300300
// Hosts may have disappeared or appeared so a complete repopulate is needed.
301301
if (m_inited) {
302302
DeviceToolBar *dt;
303-
for (size_t i = 0; i < gAudacityProjects.GetCount(); i++) {
303+
for (size_t i = 0; i < gAudacityProjects.size(); i++) {
304304
dt = gAudacityProjects[i]->GetDeviceToolBar();
305305
dt->RefillCombos();
306306
}

‎src/MemoryX.h‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,15 +670,42 @@ template<typename T, typename Deleter> class movable_ptr_with_deleter
670670
movable_ptr_with_deleter() {};
671671
movable_ptr_with_deleter(T* p, const Deleter &d)
672672
: movable_ptr_with_deleter_base<T, Deleter>( p, d ) {}
673-
movable_ptr_with_deleter(const movable_ptr_with_deleter &that) = default;
673+
674+
#ifdef __AUDACITY_OLD_STD__
675+
676+
// copy
677+
movable_ptr_with_deleter(const movable_ptr_with_deleter &that)
678+
: movable_ptr_with_deleter_base < T, Deleter > ( that )
679+
{
680+
}
681+
682+
movable_ptr_with_deleter &operator= (const movable_ptr_with_deleter& that)
683+
{
684+
if (this != &that) {
685+
((movable_ptr_with_deleter_base<T, Deleter>&)(*this)) =
686+
that;
687+
}
688+
return *this;
689+
}
690+
691+
#else
692+
693+
// move
694+
movable_ptr_with_deleter(movable_ptr_with_deleter &&that)
695+
: movable_ptr_with_deleter_base < T, Deleter > ( std::move(that) )
696+
{
697+
}
698+
674699
movable_ptr_with_deleter &operator= (movable_ptr_with_deleter&& that)
675700
{
676701
if (this != &that) {
677702
((movable_ptr_with_deleter_base<T, Deleter>&)(*this)) =
678-
std::move(that);
703+
std::move(that);
679704
}
680705
return *this;
681706
}
707+
708+
#endif
682709
};
683710

684711
template<typename T, typename Deleter, typename... Args>

‎src/Menus.cpp‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,7 @@ void AudacityProject::ModifyAllProjectToolbarMenus()
18281828
{
18291829
AProjectArray::iterator i;
18301830
for (i = gAudacityProjects.begin(); i != gAudacityProjects.end(); ++i) {
1831-
((AudacityProject *)*i)->ModifyToolbarMenus();
1831+
(*i)->ModifyToolbarMenus();
18321832
}
18331833
}
18341834

@@ -2260,9 +2260,9 @@ void AudacityProject::OnPlayStop()
22602260

22612261
//find out which project we need;
22622262
AudacityProject* otherProject = NULL;
2263-
for(unsigned i=0; i<gAudacityProjects.GetCount(); i++) {
2263+
for(unsigned i=0; i<gAudacityProjects.size(); i++) {
22642264
if(gAudioIO->IsStreamActive(gAudacityProjects[i]->GetAudioIOToken())) {
2265-
otherProject=gAudacityProjects[i];
2265+
otherProject=gAudacityProjects[i].get();
22662266
break;
22672267
}
22682268
}
@@ -3554,8 +3554,8 @@ void AudacityProject::OnManagePluginsMenu(EffectType type)
35543554
{
35553555
if (PluginManager::Get().ShowManager(this, type))
35563556
{
3557-
for (size_t i = 0; i < gAudacityProjects.GetCount(); i++) {
3558-
AudacityProject *p = gAudacityProjects[i];
3557+
for (size_t i = 0; i < gAudacityProjects.size(); i++) {
3558+
AudacityProject *p = gAudacityProjects[i].get();
35593559

35603560
p->RebuildMenuBar();
35613561
#if defined(__WXGTK__)
@@ -3835,8 +3835,8 @@ void AudacityProject::OnPreferences()
38353835
// LL: Moved from PrefsDialog since wxWidgets on OSX can't deal with
38363836
// rebuilding the menus while the PrefsDialog is still in the modal
38373837
// state.
3838-
for (size_t i = 0; i < gAudacityProjects.GetCount(); i++) {
3839-
AudacityProject *p = gAudacityProjects[i];
3838+
for (size_t i = 0; i < gAudacityProjects.size(); i++) {
3839+
AudacityProject *p = gAudacityProjects[i].get();
38403840

38413841
p->RebuildMenuBar();
38423842
p->RebuildOtherMenus();

‎src/MenusMac.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void AudacityProject::OnMacMinimize()
4646
void AudacityProject::OnMacMinimizeAll()
4747
{
4848
for (const auto project : gAudacityProjects) {
49-
DoMacMinimize(project);
49+
DoMacMinimize(project.get());
5050
}
5151
}
5252

‎src/Project.cpp‎

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,14 @@ AudacityProject *CreateNewAudacityProject()
527527
GetNextWindowPlacement(&wndRect, &bMaximized, &bIconized);
528528

529529
//Create and show a NEW project
530-
AudacityProject *p = new AudacityProject(NULL, -1,
531-
wxDefaultPosition,
532-
wxSize(wndRect.width, wndRect.height));
533-
gAudacityProjects.Add(p);
530+
gAudacityProjects.push_back(
531+
make_movable_with_deleter<AudacityProject, Destroyer< AudacityProject > >
532+
({},
533+
nullptr, -1,
534+
wxDefaultPosition,
535+
wxSize(wndRect.width, wndRect.height))
536+
);
537+
const auto p = gAudacityProjects.back().get();
534538

535539
// wxGTK3 seems to need to require creating the window using default position
536540
// and then manually positioning it.
@@ -563,21 +567,21 @@ AudacityProject *CreateNewAudacityProject()
563567

564568
void RedrawAllProjects()
565569
{
566-
size_t len = gAudacityProjects.GetCount();
570+
size_t len = gAudacityProjects.size();
567571
for (size_t i = 0; i < len; i++)
568572
gAudacityProjects[i]->RedrawProject();
569573
}
570574

571575
void RefreshCursorForAllProjects()
572576
{
573-
size_t len = gAudacityProjects.GetCount();
577+
size_t len = gAudacityProjects.size();
574578
for (size_t i = 0; i < len; i++)
575579
gAudacityProjects[i]->RefreshCursor();
576580
}
577581

578582
AUDACITY_DLL_API void CloseAllProjects()
579583
{
580-
size_t len = gAudacityProjects.GetCount();
584+
size_t len = gAudacityProjects.size();
581585
for (size_t i = 0; i < len; i++)
582586
gAudacityProjects[i]->Close();
583587

@@ -712,7 +716,7 @@ void GetNextWindowPlacement(wxRect *nextRect, bool *pMaximized, bool *pIconized)
712716
windowRect = defaultRect;
713717
}
714718

715-
if (gAudacityProjects.IsEmpty()) {
719+
if (gAudacityProjects.empty()) {
716720
if (*pMaximized || *pIconized) {
717721
*nextRect = normalRect;
718722
}
@@ -726,11 +730,11 @@ void GetNextWindowPlacement(wxRect *nextRect, bool *pMaximized, bool *pIconized)
726730
else {
727731
bool validWindowSize = false;
728732
AudacityProject * validProject = NULL;
729-
size_t numProjects = gAudacityProjects.Count();
733+
size_t numProjects = gAudacityProjects.size();
730734
for (int i = numProjects; i > 0 ; i--) {
731735
if (!gAudacityProjects[i-1]->IsIconized()) {
732736
validWindowSize = true;
733-
validProject = gAudacityProjects[i-1];
737+
validProject = gAudacityProjects[i-1].get();
734738
break;
735739
}
736740
}
@@ -1967,7 +1971,7 @@ void AudacityProject::OnIconize(wxIconizeEvent &event)
19671971

19681972
unsigned int i;
19691973

1970-
for(i=0;i<gAudacityProjects.Count();i++){
1974+
for(i=0;i<gAudacityProjects.size();i++){
19711975
if(gAudacityProjects[i]){
19721976
if( !gAudacityProjects[i]->mIconized )
19731977
VisibleProjectCount++;
@@ -2365,7 +2369,7 @@ void AudacityProject::OnCloseWindow(wxCloseEvent & event)
23652369

23662370
// DanH: If we're definitely about to quit, DELETE the clipboard.
23672371
// Doing this after Deref'ing the DirManager causes problems.
2368-
if ((gAudacityProjects.GetCount() == 1) && (quitOnClose || gIsQuitting))
2372+
if ((gAudacityProjects.size() == 1) && (quitOnClose || gIsQuitting))
23692373
DeleteClipboard();
23702374

23712375
// JKC: For Win98 and Linux do not detach the menu bar.
@@ -2445,15 +2449,21 @@ void AudacityProject::OnCloseWindow(wxCloseEvent & event)
24452449
// have been deleted before this.
24462450
mDirManager.reset();
24472451

2452+
AProjectHolder pSelf;
24482453
{
24492454
ODLocker locker{ &AudacityProject::AllProjectDeleteMutex() };
2450-
gAudacityProjects.Remove(this);
2455+
auto end = gAudacityProjects.end();
2456+
auto it = std::find_if(gAudacityProjects.begin(), end,
2457+
[this] (const AProjectHolder &p) { return p.get() == this; });
2458+
wxASSERT( it != end );
2459+
pSelf = std::move( *it );
2460+
gAudacityProjects.erase(it);
24512461
}
24522462

24532463
if (gActiveProject == this) {
24542464
// Find a NEW active project
2455-
if (gAudacityProjects.Count() > 0) {
2456-
SetActiveProject(gAudacityProjects[0]);
2465+
if (gAudacityProjects.size() > 0) {
2466+
SetActiveProject(gAudacityProjects[0].get());
24572467
}
24582468
else {
24592469
SetActiveProject(NULL);
@@ -2466,7 +2476,7 @@ void AudacityProject::OnCloseWindow(wxCloseEvent & event)
24662476
gAudioIO->SetListener(gActiveProject);
24672477
}
24682478

2469-
if (gAudacityProjects.IsEmpty() && !gIsQuitting) {
2479+
if (gAudacityProjects.empty() && !gIsQuitting) {
24702480

24712481
#if !defined(__WXMAC__)
24722482
if (quitOnClose) {
@@ -2484,7 +2494,8 @@ void AudacityProject::OnCloseWindow(wxCloseEvent & event)
24842494
NULL,
24852495
&mViewInfo);
24862496

2487-
Destroy();
2497+
// Destroys this
2498+
pSelf.reset();
24882499
mRuler = nullptr;
24892500

24902501
mIsBeingDeleted = true;
@@ -2611,7 +2622,7 @@ wxArrayString AudacityProject::ShowOpenDialog(const wxString &extraformat, const
26112622
bool AudacityProject::IsAlreadyOpen(const wxString & projPathName)
26122623
{
26132624
const wxFileName newProjPathName(projPathName);
2614-
size_t numProjects = gAudacityProjects.Count();
2625+
size_t numProjects = gAudacityProjects.size();
26152626
for (size_t i = 0; i < numProjects; i++)
26162627
{
26172628
if (newProjPathName.SameAs(gAudacityProjects[i]->mFileName))
@@ -5332,7 +5343,7 @@ bool AudacityProject::ExportFromTimerRecording(wxFileName fnFile, int iFormat, i
53325343
}
53335344

53345345
int AudacityProject::GetOpenProjectCount() {
5335-
return gAudacityProjects.Count();
5346+
return gAudacityProjects.size();
53365347
}
53375348

53385349
bool AudacityProject::IsProjectSaved() {

0 commit comments

Comments
 (0)