I've been creating a class that takes a bunch of images and overlays them onto one BMP. For some reason upon running the code I'm getting a segfault and I've tracked it down to this method. Essentially the if statement checks to see if there is a valid index in the array of images to place this new image in. If it is valid then it deletes whatever was there previously and sets that index to this new Image. The Class is called Scene and is comprised of an array of Image pointers. So what I'm doing is replacing the image that one of those pointers points to. Somehow its not working though. If the pointer is NULL the delete command shouldn't cause any problems, so I don't see what could be going wrong. This code is acting on a Scene that has an array of Image pointers of length 5.
void Scene::addpicture(const char* FileName, int index, int x, int y)
{
if (index<0 || index>maxnum-1)
{
cout << "index out of bounds" << endl;
}
else
{
Image* extra;
extra = new Image;
extra->ReadFromFile(FileName);
delete imagelist[index];
imagelist[index] = extra;
imagelist[index]->xcoord=x;
imagelist[index]->ycoord=y;
}
}
Can anyone help. It would be much appreciated.
Thanks
I've edited to include the constructor:
Scene::Scene(int max)
{
Image** imagelist = new Image*[max];
for(int i=0; i<max; i++)
{imagelist[i] = NULL;}
maxnum = max;
}
I've also commented out the main method so that the only functions being called are
Scene* set = new Scene(5);
set->addpicture("in_01.bmp", 0, 0, 0);
In your constructor you have a local imagelist, but you are using a field imagelist in addpicture. You are shadowing the imagelist field in the constructor and the field never gets initialized.
Fix it by replacing this line:
Image** imagelist = new Image*[max];
With this:
imagelist = new Image*[max];
A SEGFAULT means that you're trying to access a location outside of what you should be. In your comment to Messa, you say that it's happening at the delete command.
So, I ask you: When you construct the Scene class, do you explicitly initialize the pointers in imagelist to NULL? In other words, are there lines like:
for (i=0; i<maxnum; i++) {
imagelist[i] = NULL;
}
in your constructor, or are you assuming that uninitialized arrays start as 0-filled? (Unlike most languages, that assumption is bad in C++.)
This code looks OK, I think error is in some other part of the program. Maybe imagelist array is not initialized to NULL? Or maxnum is not the actual size of imagelist. Or something other.
What exactly is failing - do you have traceback?
Related
In My class I have a member variable;
QProcess* p1;
Inside some function, i initialize and use it as:
process1 = new QProcess();
it works fine. Now i have a situation where i have many of these processes to be started. One option is to declare all of them as member functions:
QProcess* p1;
QProcess* p2;
QProcess* p3;
...
And then initialize all of them when needed. However this is too much redundant work. So i tried to create a list and initialize it in a loop like this:
QList<QProcess*> procList;
for(int i=0; i<len; i++){
procList[i] = new QProcess();
}
It compiles fine but then crashes. Is there something missing or what am i doing wrong here?
I also tried to add all member variables in this list like:
for(int i=0; i<len; i++){
switch(i){
case 0:
procList[i] = p1;
break;
}
}
But this also has the same result like above
EDIT:
Based on your suggestion, i tried:
procList.append(new QProcess());
as well as procList.append(p1);
But result is same, it compiles but crashes at run time
EDIT:
So i found the issue was totally unrelated. The class itself where i was using this code (custom class i made) had no default constructor. And as i learned without default constructor, if you initialize a pointer, somehow it just crashes.... strange. After specifying default constructor it is working fine now.
You are accessing areas of the memory that is not allocated. Your list is empty and you access one element that is not valid.
As per documentation: https://doc.qt.io/qt-5/qlist.html#operator-5b-5d
T &QList::operator[](int i) Returns the item at index position i as a
modifiable reference. i must be a valid index position in the list
(i.e., 0 <= i < size()).
If this function is called on a list that is currently being shared,
it will trigger a copy of all elements. Otherwise, this function runs
in constant time. If you do not want to modify the list you should use
QList::at().
Try to use append or push_back, see https://doc.qt.io/qt-5/qlist.html#push_back
PS: Not an expert of QT, but you might want to look into a QList of std::unique_ptr to manage your memory if possible. Otherwise you risk to forget to delete the heap-allocated elements whose pointer is stored in the list.
Edit: OP reports that the suggestion did not work. I wrote a small example myself (disregarding possible leaks). The following example crashes when using operator[] but works with append in debug mode (I used QT creator for Windows with Qt 5.13.0 for MinGW 64 bits) . What OP is experiencing can be either some issue with the toolchain or some undefined behavior triggered before the append. I suggest OP tries to copy/paste my code in a clean project and run it.
#include <QList>
#include <QProcess>
int main()
{
QList<QProcess*> list;
for(int i = 0; i < 10; ++i){
QProcess * p = new QProcess();
//Decomment and crash
//list[i] = p;
//does not crash
list.append(p);
}
//Here you should cleanup
}
I have a Class "Grid" which declares a 2-dimensional array of pointers to objects of class "Cell" as property. When I instantiate an object of "Grid" I see in the debugger, that the constructor's code which initializes the array calling "new Cell(p1, p2)" inside a loop produces pointers to Cells which lie inside the allocation for the array, thus corrupting the array of pointers.
Here some extracts of the code:
class Cell {
public:
Cell( int a, int b );
}
class Grid {
public:
Grid();
Cell * g[6][7];
}
// code:
Grid::Grid() {
// g is allocated to address x'13d1-19b8 (extending for 42*4=168 bytes)
for ( int r=0; r<6; r++ )
{
for ( int c=0; c<7; c++ )
{
g[r][c] = new Cell( r, c ); // g[0][0] now contains x'13d1-19d8 which is INSIDE g[][] !?!?!
}
}
.....
}
Can anybody tell me what's going on or what I'm doing wrong?
I've rebuilt the project piece by piece, trying to descover which part of the code causes the corruption. However I couldn't reproduce the error. I now have what looks to my eye an identical copy of the original code and it works just fine. (The original one still fails.) Can't see where I made the mistake in the first attempt, but I'm happy to be able to progress.
Thanks to those (#Cameron) who tried to help and maybe some day #Dieter will explain to me why he down-voted my question... ;)
I have these scenarios and I want to know if I manage my memory correctly. I watch the memory consumption in the Task Manager when I start the executable and see how memory is not popped back to the initial amount, which leads me to suspect that I don't clear memory where is needed.
So, in this first case I have a function that adds a new element to a dynamic array:
struct Color {
int R;
int G;
int B;
}
int TotalColors;
Color* Rainbow;
void AddColor(Color NewColor) {
// So, I create a new array of size TotalColors+1
Color* NewRainbow = new Color[TotalColors+1];
// Now I add the existing elements
for (int i=0; i<TotalColors; i++) {
NewRainbow[i] = Rainbow[i];
}
// And lastly, I add the new element
NewRainbow[TotalColors] = NewColor;
// Now, I assign the NewRainbow to Rainbow (I don't know if it's correct)
Rainbow = NewRainbow;
}
So, in this case, do you think I miss something? This is working but I want to make sure the unused stuff is removed from memory.
I also have a function to remove an element, which looks like this:
void RemoveColor(Color Removable) {
// Again, I create a new array of size TotalColors-1
Color* NewRainbow = new Color[TotalColors-1];
// I scan the list and add only those elements which are not 'Removable'
for (int i=0; i<TotalColors; i++) {
// Let's suppose that Removable exists in the list
if (Rainbow[i].R != Removable.R && Raibow[i].G != Removable.G && ... {
NewRainbow [i] = Rainbow[i];
}
}
// Again, the same operation as above
NewRainbow[TotalColors] = NewColor;
Rainbow = NewRainbow;
}
In this case, I don't know what happens with Rainbow[Removable], I mean, the element of the array that is removed.
And the last case, is this, where I try to send the pointer of an element from the array to a function.
Color* GetColor(int Index) {
Color* FoundColor;
// Scan the array
for (int i=0; i<TotalColors; i++) {
if (i == Index) FoundColor = &Rainbow[i];
}
return FoundColor;
}
// And I use it like this
void ChangeColor(int Index) {
Color* Changeable;
Changeable = GetColor(Index);
SetRGB(Changeable, 100, 100, 100);
}
// And this is what changes the value
void SetRGB(Color* OldRGB, int R, int G, int B) {
(*oldRGB).R = R;
(*oldRGB).G = G;
(*oldRGB).B = B;
}
And this is it. So, this works but I am not sure if with so many pointers I didn't forget to delete something. For example, when I RemoveColor I don't see the memory changed (maybe some bytes don't make the difference) and I just want some professional eye to tell me if I missed something. Thanks!
In the first function AddColor() you are not deleting the previously allocated memory.
Rainbow = NewRainbow; // leaking the memory Rainbow was previously pointing to.
Change that last line to:
delete[] Rainbow;
Rainbow = NewRainbow;
Same thing with RemoveColor()
Any time you use the new operator it needs to have a corresponding delete. Also, if you are allocating an array with new[] as in your case, it must have a corresponding delete[].
In order not to worry whether you've forgotten to delete a pointer, you shouldn't use plain pointers. Instead, use smart pointers such as
std::shared_ptr
std::unique_ptr
etc.
or, if you don't have C++11 yet, use
boost::shared_ptr
boost::scoped_ptr
More on smart pointers, see Wikipedia and the specific documentation.
I'm compiling using Code::Blocks on Windows 7 using the MinGW compiler (which I can only assume is the latest version; both Code::Blocks and MinGW were installed this past week). My issue crops up under a particular circumstance, and my attempts to write a simpler script that demonstrates the problem have failed (which implies that there is something wrong with my structure). Also, my apologies for how long this post is.
Currently, I'm rolling with one class, FXSDL, which will act as an SDL wrapper:
class FXSDL
{
public:
FXSDL();
virtual ~FXSDL();
int Initialize();
int Render();
FXID CreateCharacter(FXID hRefID, string fpImage, int wpxTile, int hpxTile, map<int, vector<int> > htAnims);
int SetAnim(FXID hRefID, FXID hAnimID);
FXID hPlayer;
protected:
private:
list<FXSurface> m_lstFXObjects;
list<FXSurface>::iterator m_liFirst;
SDL_Surface* m_lpsfSDLScreen;
Uint32 m_tmOld;
Uint32 m_tmFrame;
};
The value type of my list is:
struct FXSurface
{
FXID hRefID;
int wpxTile;
int hpxTile;
int wpxTotal;
int hpxTotal;
int cntTiles;
map<int, vector<int> > htAnims; // All animations
map<int, vector<int> >::iterator vCurr; // Currently active animation
vector<int>::iterator fiCurr; // Currently active frame
SDL_Surface* lpsfSDL;
SDL_Rect* lprcTiles; // Predefined frame positions
string* fpImage;
};
I've implemented very simple initialize and render function. The CreateCharacter function takes a few parameters, the most important of which is htAnims, a map of integer vectors (idea being: I define numeric ids with easy-to-remember representations, such as FXA_IDLE or FXA_WALK, as the key, and the series of number values representing frames for the animation as a vector). This could be fairly easily implemented as a multidimensional integer array, but animations are variable in length and I want to be able to add new anims (or redefine existing ones) without having to recast an array.
The CreateCharacter function is simple. It creates a new FXSurface, populates it with the required data, and pushes the new FXSurface onto the list:
FXID FXSDL::CreateCharacter(FXID hRefID, string fpImage, int wpxTile, int hpxTile, map<int, vector<int> > htAnims)
{
//list<FXSurface>::iterator lpsfTemp;
FXSurface lpsfTemp;
list<FXSurface>::iterator lpsfPos;
SDL_Rect* lprcCurr = NULL;
int cntTileW = 0;
int cntTileH = 0;
int cntCurr = 0;
// Start off by initializing our container struct
//lpsfTemp = new FXSurface();
lpsfTemp.lpsfSDL = IMG_Load(fpImage.c_str()); // Try to load the requested image
if(lpsfTemp.lpsfSDL != NULL) // If we didn't fail to
{
// Assign some variables for tracking
lpsfTemp.hRefID = hRefID;
lpsfTemp.fpImage = &fpImage;
lpsfTemp.wpxTotal = lpsfTemp.lpsfSDL->w;
lpsfTemp.hpxTotal = lpsfTemp.lpsfSDL->h;
// If a tile width was specified, use it
if(wpxTile != 0)
{
lpsfTemp.wpxTile = wpxTile;
lpsfTemp.hpxTile = hpxTile;
} // Otherwise, assume one tile
else
{
lpsfTemp.wpxTile = lpsfTemp.wpxTotal;
lpsfTemp.hpxTile = lpsfTemp.hpxTotal;
}
// Determine the tiles per row and column for later
cntTileW = lpsfTemp.wpxTotal / lpsfTemp.wpxTile;
cntTileH = lpsfTemp.hpxTotal / lpsfTemp.hpxTile;
// And the total number of tiles
lpsfTemp.cntTiles = cntTileW * cntTileH;
lpsfTemp.lprcTiles = new SDL_Rect[cntTileW*cntTileH];
// So we don't calculate this every time, determine each frame's coordinates and store them
for(int h = 0; h < cntTileH; h++)
{
for(int w = 0; w < cntTileW; w++)
{
cntCurr = (h*cntTileW)+w;
lprcCurr = new SDL_Rect;
lprcCurr->w = lpsfTemp.wpxTile;
lprcCurr->h = lpsfTemp.hpxTile;
lprcCurr->x = w*lpsfTemp.wpxTile;
lprcCurr->y = h*lpsfTemp.hpxTile;
lpsfTemp.lprcTiles[cntCurr] = *lprcCurr;
lprcCurr = NULL;
}
}
// Now acquire our list of animations and set the default
//lpsfTemp.htAnims = new map<int, vector<int> >(*htAnims);
lpsfTemp.htAnims = htAnims;
lpsfTemp.vCurr = lpsfTemp.htAnims.find(FXA_WALK_EAST);
lpsfTemp.fiCurr = lpsfTemp.vCurr->second.begin();
this->m_lstFXObjects.push_back(lpsfTemp);
}
else
{
hRefID = 0;
}
return hRefID;
}
It is precisely as the object is pushed that the error occurs. I've stepped through the code numerous times. Initially, I was only able to tell that my iterators were unable to dereference to the FXSurface object. After using watches to identify the exact memory address that the iterator and list objects pointed to, and dereferencing the address, I noticed the reason for my segfaults: all the values which I put into the original FXSurface were pushed down two memory blocks when the list object copied it!
My process for doing this is simple. I set up a breakpoint at the return statement for CreateCharacter, which gives me a view of lpsfTemp (the FXSurface I later add to the list) and m_lstFXObjects (the list I add it to). I scroll through the members of m_lstFXObjects, which brings me to _M_node, which contains the memory address of the only object I have added so far. I add a watch to this address in the form of (FXSurface)-hex address here-
First, find the address:
(There should be a picture here showing the highlighted _M_node attribute containing the list item's address, but I can't post pictures, and I can only post one URL. The second one is by far more important. It's located at http://www.fauxsoup.net/so/address.jpg)
Next, we cast and deference the address. This image shows both lpsfTemp and the copy in m_lstFXObjects; notice the discrepancy?
http://www.fauxsoup.net/so/dereferenced.jpg - See? All the values are in the correct order, just offset by two listings
I had initially been storing fpImages as a char*, so I thought that may have been throwing things off, but now it's just a pointer and the problem persists. Perhaps this is due to the map<int, vector<int> > I store?
FXSDL has a destructor, but no copy constructor and no assignment operator. Yo you're using naked pointers, but violate the Rule of Three.
I'm not going to look any further.
Use smart pointers to manage resources. Do not put a naked resource into a type, except when that type's only intention is to manage this one resource. From another answer given yesterday:
As a rule of thumb: If you have to manually manage resources, wrap each into its own object.
At a glance, I'd say you're double-deleting lpsfSDL and/or lprcTiles. When you have raw pointers in your structure, you need to follow the rule-of-three (implement copy constructor, assignment operator, and destructor) to properly manage the memory.
These lines look wrong to me:
lprcCurr = new SDL_Rect;
lprcCurr->w = lpsfTemp.wpxTile;
lprcCurr->h = lpsfTemp.hpxTile;
lprcCurr->x = w*lpsfTemp.wpxTile;
lprcCurr->y = h*lpsfTemp.hpxTile;
lpsfTemp.lprcTiles[cntCurr] = *lprcCurr;
lprcCurr = NULL;
lpsfTemp.lprcTiles is a SDL_Rect*. lprcTemp.lprcTiles[cntCurr] is a SDL_Rect. You should be writing this, IMHO:
SDL_Rect tmpRect;
tmpRect.w = lpsfTemp.wpxTile;
tmpRect.h = lpsfTemp.hpxTile;
tmpRect.x = w*lpsfTemp.wpxTile;
tmpRect.y = h*lpsfTemp.hpxTile;
lpsfTemp.lprcTiles[cntCurr] = tmpRect;
Dump the lprcCurr entirely.
Now this code:
lpsfTemp.vCurr = lpsfTemp.htAnims.find(FXA_WALK_EAST);
lpsfTemp.fiCurr = lpsfTemp.vCurr->second.begin();
This is bad. These iterators are invalid as soon as the push_back completes. That push_back is making a copy of lpsfTemp. The map and vector members are going to copy themselves and those iterators will copy themselves but they will be pointing to lpsfTemp's members which are going to be destroyed as soon as CreateCharacter exits.
One way to fix that would be to push_back a FXSurface object at the beginning, use back() to get its reference and operate on that instead of lpsfTemp. Then the iterators would stay consistent and they should stay consistent since you are using a list which does not copy its objects around. If you were using a vector or deque or anything other than a list you would need to manage all those pointers and iterators in the copy constructor and assignment operator.
Another thing: Double and triple check your array bounds when you access that lprcTiles array. Any mistake there and you could be scribbling over who knows what.
I don't know if any of that will help you.
I'm having an issue where using vector.push_back(value) is overwriting the final value, rather than appending to the end. Why might this happen? I have a sample item in the vector, so it's size never hits zero. Below is the code..
void UpdateTable(vector<MyStruct> *Individuals, MyStruct entry)
{
MyStruct someEntry;
bool isNewEntry = true;
for (int i = 0; i < Individuals->size(); i++)
{
if (!(strcmp(Individuals->at(i).sourceAddress, entry.sourceAddress)))
{
isNewEntry = false;
//snip. some work done here.
}
}
if(isNewEntry)
{
Individuals->push_back(entry);
}
}
This let's my first "sample" value stay in, and will allow for just one more item in the vector. When 2 new entries are added, the second overwrites the first, so the size is never larger than 2.
edit: More code, since this is apparently not the issue?
void *TableManagement(void *arg)
{
//NDP table to store discovered devices.
//Filled with a row of sample data.
vector<MyStruct> discoveryTable;
MyStruct sample;
sample.sourceAddress = "Sample";
sample.lastSeen = -1;
sample.beaconReceived = 1;
discoveryTable.push_back(sample);
srand(time(NULL));
while(1)
{
int sleepTime = rand() % 3;
sleep(sleepTime);
MyStruct newDiscovery = ReceivedValue();
if (newDiscovery.lastSeen != -1000) //no new value from receivedValue()
{
UpdateTable(&discoveryTable, newDiscovery);
}
printTable(&discoveryTable);
}
return NULL;
}
I'm going to hazard a guess:
Suppose MyStruct is declared like
struct MyStruct
{
const char *sourceAddress;
// Other Gubbins ...
};
And that ReceivedValue does something like
MyStruct ReceivedValue()
{
static char nameBuffer[MAX_NAME_LEN];
// Do some work to get the value, put the name in the buffer
MyStruct s;
s.sourceAddress = nameBuffer;
// Fill out the rest of MyStruct
return s;
}
Now, every structure you push into your vector has sourceAddress pointing to the same global buffer, every time you call ReceivedValue it overwrites that buffer with the new string - so every entry in your vector ends up with the same string.
I can't be sure without seeing the rest of your code, but I can be sure that if you follow some of the good C++ style suggestions in the comments to your question this possiblity would go away.
Edit for clarification: there's no need to heap allocate your structures, simply declaring sourceAddress as a std::string would be sufficient to eliminate this possibility.
The scope for the items you are pushing into the database is expiring. They're being destructed when you leave the {} in which they were created - and as such the reference to them is no longer valid.
You need to change it from vector<MyStruct> to vector<MyStruct*> (preferably using safe pointers from Boost:: instead of pointers, but you get the idea).
You are creating the item within the (limited) scope and pushing it onto the vector (while the struct is copied, the strings in it are not!) it then reuses the same memory location (most likely if properly optimized) to store the next "new" struct and the one after that and so on and so forth.
Instead, within the limited scope create MyStruct *myObject = new MyStruct and assign its values, then push the pointer to the vector.
Remember to delete all values from the vector before clearing it/destroying it!!
Or, of course, you could use std::string/CString/whatever instead of a char array and avoid the issue entirely by having a safe-to-copy struct.
ComputerGuru's answer works however there in another alternative. You can create a copy constructor and overload operator= for MyStruct. In these operations, you need to copy the actual string into the new struct. In C++, structs are nothing more than classes with default public access instead of default private access. Another alternative is to use std::string instead of char* for the string value. C++ strings already have this behavior.
struct MyStruct {
std::string sourceAddress;
int lastSeen;
int beaconReceived;
};
Seems odd to me: Maybe there is something wrong with the //snip part of the code?
Try to log the size of the vector before and after the push_back call (either in the debugger or using cout) and also have a look at the isNewEntry variable.
Your code looks alright to me. Is it possible that you are not passing the right vector in? What I mean is that the behaviour you describe would appear if somehow your Individuals vector is being reset to its orginal 1-entry state before you tried to add the 3rd entry, then it would appear as if your 2nd entry was being overwritten.
Here is what I mean:
int test_table()
{
string SampleAddresses[] = {"Sample Address 1", "Sample Address 2"};
for (int i = 0; i < 2; i++)
{
// All this work to build the table *should* be done outside the loop; but we've accidentally put it inside
// So the 2nd time around we will destroy all the work we did the 1st time
vector<MyStruct> Individuals;
MyStruct Sample;
Sample.sourceAddress = "Sample Address 0";
Test.push_back(Sample);
// this is all we meant to have in the loop
MyStruct NewEntry;
NewEntry.sourceAddress = SampleAddresses[i];
UpdateTable(Individuals, NewEntry);
}
//Now the table has 2 entries - Sample Address 0 and Sample Address 2.
}
If this was all your code then the problem would be obvious. But it might be concealed in some other pieces of code.