Deallocate memory of wxGrid

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Eman
Knows some wx things
Knows some wx things
Posts: 38
Joined: Sat Aug 31, 2019 2:11 pm

Deallocate memory of wxGrid

Post by Eman » Fri Oct 09, 2020 9:31 pm

Hi
I have a table:
wxGrid *gridTable;
gridTable = new wxGrid(panel,-1,wxPoint( 0, 0 ), wxSize( 512, 512 ));
gridTable->CreateGrid(height, width );

when the user choose a new height and width (either smaller or larger than the previous ones), I want to de-allocate memory and create a table with new size:
delete gridTable;
gridTable = new wxGrid(panel,-1,wxPoint( 0, 0 ), wxSize( 512, 512 ));
gridTable->CreateGrid(newHeight, newWidth );

My problem is that delete gridTable; caused run time error, I looked at the functions of wxGrid, but I cannot one to de-allocate memory.

User avatar
doublemax
Moderator
Moderator
Posts: 15283
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Deallocate memory of wxGrid

Post by doublemax » Fri Oct 09, 2020 10:17 pm

Recreating a control in-place is always a little tricky. Maybe it's easier to add/remove cols/rows with the respective methods to match the new row/col count. (Or delete all rows/cols and then add new ones from scratch).
wxGrid::DeleteCols
wxGrid::DeleteRows
wxGrid::AppendCols
wxGrid::AppendRows
Afterwards call wxGrid::ClearGrid
Use the source, Luke!

Eman
Knows some wx things
Knows some wx things
Posts: 38
Joined: Sat Aug 31, 2019 2:11 pm

Re: Deallocate memory of wxGrid

Post by Eman » Sat Oct 10, 2020 1:37 am

unfortunately,
The program crash and stop execution when reaching this line:

Code: Select all

gridTable->DeleteCols(0,gridTable->GetNumberRows(),false);
The line before it is:

Code: Select all

			cout<<"# of cols = "<<gridTable->GetNumberRows()<<endl;
Give me correct output, which is 8 (the number of cols)

Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 251
Joined: Tue Jun 07, 2016 1:07 pm

Re: Deallocate memory of wxGrid

Post by Kvaz1r » Sat Oct 10, 2020 6:22 am

Could you provide reproducible sample of code?

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2675
Joined: Sun Jan 03, 2010 5:45 pm

Re: Deallocate memory of wxGrid

Post by PB » Sat Oct 10, 2020 7:10 am

Eman wrote:
Sat Oct 10, 2020 1:37 am
The program crash and stop execution when reaching this line:

Code: Select all

gridTable->DeleteCols(0,gridTable->GetNumberRows(),false);
The line before it is:

Code: Select all

			cout<<"# of cols = "<<gridTable->GetNumberRows()<<endl;
Give me correct output, which is 8 (the number of cols)
I am confused, the second parameter of DeleteCols() is the number of columns, not rows (that would be for DeleteRows()). And why would you ask for the number of columns with GetNumberOfRows()?

I would not do anything like this even if I were absolute sure that the number of columns always equals number of rows.

Eman
Knows some wx things
Knows some wx things
Posts: 38
Joined: Sat Aug 31, 2019 2:11 pm

Re: Deallocate memory of wxGrid

Post by Eman » Thu Oct 15, 2020 5:19 pm

The code is very long as it contains many cpp and h files:
Below is part of the code:

Code: Select all

  int height = 8;
                int width =8;
	        wxGrid *gridTable; // table
		gridTable = new wxGrid(panel,-1,wxPoint( 0, 0 ), wxSize( 550, 550 ));
		gridTable->CreateGrid(height, width );
		gridTable->SetDefaultRowSize(5);
		gridTable->SetDefaultColSize(5);
		gridTable->SetRowLabelSize(0);
		gridTable->SetColLabelSize(0);
                // The table is shown without any errors
                //  the OnChoice function is called when the size's drop down menu is changed
                // free the memory allocated by gridTable, so I can create a new table either smaller or bigger than the existing one
               	gridTable->DeleteCols(0,gridTable->GetNumberCols(),false);  // the program crash when reaching this line

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2675
Joined: Sun Jan 03, 2010 5:45 pm

Re: Deallocate memory of wxGrid

Post by PB » Thu Oct 15, 2020 5:25 pm

Without having seen the access code, I can have only two guesses for the most common mistakes

1. Shadowed variable

Code: Select all

class MyFrame ...
{
wxGrid* gridTable;

MyFrame()
{
// local variable shadows the member variable
wxGrid* gridTable = new ....
}

void MyHandler()
{
   use uninitialized member variable gridTable here, leading to crash
}

}
2. Using a wrong event sink when connecting the event handler, which can happen only when using Connect() instead of Bind(). The code explodes as soon as it attempts to access any member variable, because it has wrong this. I have never used Connect() but I think you probably can even use a method with a wrong signature there.

Of course, it can be anything else, hard to say. If the debugger is of no help, you need to the usual, i.e., start cutting out the code and see when it starts working.

Eman
Knows some wx things
Knows some wx things
Posts: 38
Joined: Sat Aug 31, 2019 2:11 pm

Re: Deallocate memory of wxGrid

Post by Eman » Thu Oct 15, 2020 5:39 pm

I added this line:

Code: Select all

cout<<"# of cols = "<<gridTable->GetNumberCols()<<endl;
Before the line:

Code: Select all

gridTable->DeleteCols(0,gridTable->GetNumberCols(),false);
cout<<"2"<<endl;
it gives correct output which is # of cols = 8
The second cout is not executed.

The error message in the console is :
/home/eman/.codelite/tmp/eman/codelite-exec.sh: line 3: 10696 Segmentation fault (core dumped) ${command}


Regarding the second suggestion, yes I am using connect to handle the choice menu events

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2675
Joined: Sun Jan 03, 2010 5:45 pm

Re: Deallocate memory of wxGrid

Post by PB » Thu Oct 15, 2020 5:53 pm

Post the Connect() call then, including context (from which class and method it is called).

Better yet, do not use obsolete Connect() and use Bind() instead.

See here for an example of using Connect() incorrectly, leading to a crash:
viewtopic.php?f=1&t=34191#p140623

But I am not saying the issue is with Connect(), you can have a bug somewhere in your code that just may make that part of it crash.

Eman
Knows some wx things
Knows some wx things
Posts: 38
Joined: Sat Aug 31, 2019 2:11 pm

Re: Deallocate memory of wxGrid

Post by Eman » Thu Oct 15, 2020 6:01 pm

the gridTable is added to the sizer:

Code: Select all

vboxMap->Add(gridTable,0,wxFIXED_MINSIZE | wxALL,10);
My code was clearing the vboxMap then delete gridTable, so I was trying to delete a pre-deleted memory:

Code: Select all

vboxMap->Clear(true);
gridTable->DeleteCols(0,gridTable->GetNumberCols(),false);
I swapped the two lines, and it works!

I have a question:
to free the memory allocated by gridTable, is it enough to DeleteCols, or I have to DeleteRows as well.

Many Thanks for you all...

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2675
Joined: Sun Jan 03, 2010 5:45 pm

Re: Deallocate memory of wxGrid

Post by PB » Thu Oct 15, 2020 6:05 pm

Can't you just use wxGrid::ClearGrid()?

Anyway, I would call DeleteRows() if I wanted to keep the columns, DeleteColumns() otherwise.

But calling wxSizer::Clear(true) deletes the windows owned by the sizer which will clear everything without you needing do anything.

Post Reply