Release fails where debug doesn't - c++

I have a problem with my code, it seems to fail in a weird way in release mode, debug mode works just fine.
I am out of ideas and clues. I would like to get a direction for what should i do.
So here is the problematic code:
static inline void FindPoint(Line *First,AABB *Second,point *Pt,int direction)
{
std::vector<point*> pntvec; ... }
When i call function FindPoint it is all fine and everything is set well.
But when i reach the vector initialization the First pointer is reset to the same address as the vector.
That causes unexpected behavior.
So i thought the problem was in the commonalities. Both Line and the vector are using the point structure.
Yet i was not able to find anything unusual.
here are the structures
typedef struct
{
double x;
double y;
}point;
typedef struct
{
double incline;
double y;
double x;
double c;
point start,finish;
}Line;
And here is the initialization of the data sent to parameter First:
Line *objvec = new Line;
so what could be the problem can any one tell?
Thank you in advance
Edit:
A better look at the code
http://pastebin.com/rki7EdM6

You're using pointers more heavily than you should for these simple types. Pointers and dynamic allocation are the likely sources of your undefined behavior. So a simple first step is to refactor and clean up your code, preferring stack variables and pass-by-reference for simple types. In all likelihood, the undefined behavior will go away, or the error will be easier to spot.
[Caveat: I can't get to pastebin, so I can't see your full code. Prefer simple examples right here on the site]
For example, your function signature should probably be this (I'm guessing about which parameter is the output)
static inline point FindPoint(const Line &First, const AABB &Second,int direction)
And
Line *objvec = new Line;
should just be
Line objvec;
etc, etc.
std::vector<point> pntvec;
is probably the most important. A vector of pointers is ok, but should be avoided if possible. One of the nice parts about standard containers is the auto-cleanup when the variable goes out of scope. You don't get that with a vector of pointers, since the vector doesn't know to call delete on every element. You have to do it yourself.

"so what could be the problem can any one tell?"
If such a situation occurs, the most probable reason is, you have hit undefined behavior in your code somewhere else.
I can't tell any more from the context and code you have posted. But using a tool like valgrind is likely to do this right on your service.

Related

Can you have too many variables in a header?

I kept getting runtime errors at one spot of my code and narrowed it down to a bool that was initialized to 205. I looked this issue up and that's apparently garbage that an uninitialized bool can have.
Okay, but I initialized it. This boolean value is in the private section of the header file. The class contains a method to set the value and another to return the value. Pretty bog standard stuff. So I set up a quick little test.
thing.setValue(false);
cout << thing.getValue() <<endl;
(not the actual names, but just to keep it simple)
It still spit out 205. Literally one statement setting the variable, then the very next simply asking to repeat it back and it's giving me garbage.
Somewhat perplexed, I took a look at the header file. There's an int declared right above the bool in the header, so I did a similar test with that one. It works just fine. I swap the order of the two in the header. Now the bool works and the int doesn't. So there's one vector<int> in the header above the two that it turns out I'm no longer using because I've replaced the method that used it. So I delete that, lo and behold it now works.
I can't seem to find any information on this. I imagine it's likely I just don't know what to search for to find the answer. I'm hoping it's a really obvious and dumb mistake someone more experienced can slap me over the head with so I can move on.
For code example, it's just a simple header file like any other that looks like
#ifndef concept_H
#define concept_H
class concept {
private:
//...
//...
//...
int x;
bool y;
public:
//bunch of methods//
void setY(bool whatever);
bool isY();
};
#endif
with the method implementation being equally trivial
void concept::setY(bool whatever){
y=whatever;
}
bool concept::isY(){
return y;
}
This isn't the actual code; there's a lot of other stuff there on the periphery and I just don't know what to include as relevant. Hoping again that it's a dumb mistake I'm making and someone can just tell me right away, but if not I can provide more. I just need help to even know where to begin to look.
No, there is no set limit on the number of variables you can declare. That is not the cause of your issue.

Segmentation fault when declaring variable inside struct

I've recently come across a very strange segfault while developing my application. Basically, if I add another variable to one of my structs, a segfault is caused upon execution, for no apparent reason. Removing this variable immediately solves the problem. The struct is as follows:
typedef struct Note {
char cNote;
unsigned int uiDuration;
unsigned int uiVelocity;
};
As soon as I add a
long lStartTime;
variable anywhere in the struct, the code compiles as usual but will throw a segmentation fault. GDB's backtrace is lost somewhere in some obscure WIN methods that I don't even use.
Any ideas?
Thanks!
I see several possible explanations:
Something somewhere assumes that the struct is of a certain size. Changing the size break things.
You might have a memory bug of some sort, which is brought to the surface by you changing the layout of things in memory. Try a tool like valgrind or Purify.
You are changing the struct in a header file, but are failing to rebuild all source files that use the struct.

Error Writing to Memory of Particular Data Member

Okay, I have a struct, TextBlock, that simulates moving blocks of text around the screen. Here's the header:
struct TextBlock
{
RECT textArea;
RECT rectArea;
double whatBlock;
double x;
double y;
double angle;
double speed;
double width;
double height;
char *word;
bool stuck;
};
When it's like this, everything works perfectly fine. The problem comes when I add another member that I need. The way it works is that I have two arrays of TextBlocks. The first is for moving ones, the second is for ones that don't move, signifying where the moving ones need to go. The words are all randomized from a sentence to a jumble, so this data member will be set (commented out) to the index of which static block belongs to the moving one so I know when it's in the right place.
int whatBlock;
After creating this, I go through all of the created objects and set
tb[i][j].whatBlock = 0; //same area as other data members being set, moving text
stb[i][j].whatBlock = 0; //static text block
When I try to run this, without doing anything else to the data member, it comes up with an error:
The instruction at [address] referenced memory at [different address]. The memory could not be "written".
Note that if I don't try to modify it, and just create the data member, it works.
At this point of almost being done and having tons of these kinds of problems, I'm getting a bit fed up with this program >.> Any help at all on this would be greatly appreciated.
EDIT: This issue is now fixed. I replied to the accepted answer with the explanation, but it poses another problem, even if it doesn't affect this program.
Force a rebuild of everything. You may have an object file that is out-of-date with respect to the header file that defines TextBlock
If that doesn't fix it, run your program under a debugger and see what the faulting instruction is. Either that will allow you to fix the program, or you can ask again with mroe informatin.
Without you posting more code, I can only tell that you likely have a memory corruption bug in your program - i.e. you are reading or writing beyond the end of allocated memory.
If you post more code, I'll edit this answer accordingly.
I can't really give advices since we cannot access the complete source code.
Anyway, I can suggest you that it may be not in the struct TextBlock where really hides the bug. For instance, every access to TextBlock's member means that you are accessing to this hidden variable.
If this pointer is corrupted, you may experience problem where you don't expect it, leading you to search in the wrong places.

Remove never-run call to templated function, get allocation error on run-time

I have a piece of templated code that is never run, but is compiled. When I remove it, another part of my program breaks.
First off, I'm a bit at a loss as to how to ask this question. So I'm going to try throwing lots of information at the problem.
Ok, so, I went to completely redesign my test project for my experimental core library thingy. I use a lot of template shenanigans in the library. When I removed the "user" code, the tests gave me a memory allocation error. After quite a bit of experimenting, I narrowed it down to this bit of code (out of a couple hundred lines):
void VOODOO(components::switchBoard &board) {
board.addComponent<using_allegro::keyInputs<'w'> >();
}
Fundementally, what's weirding me out is that it appears that the act of compiling this function (and the template function it then uses, and the template functions those then use...), makes this bug not appear. This code is not being run. Similar code (the same, but for different key vals) occurs elsewhere, but is within Boost TDD code.
I realize I certainly haven't given enough information for you to solve it for me; I tried, but it more-or-less spirals into most of the code base. I think I'm most looking for "here's what the problem could be", "here's where to look", etc. There's something that's happening during compile because of this line, but I don't know enough about that step to begin looking.
Sooo, how can a (presumably) compilied, but never actually run, bit of templated code, when removed, cause another part of code to fail?
Error:
Unhandled exceptionat 0x6fe731ea (msvcr90d.dll) in Switchboard.exe:
0xC0000005: Access violation reading location 0xcdcdcdc1.
Callstack:
operator delete(void * pUser Data)
allocator< class name related to key inputs callbacks >::deallocate
vector< same class >::_Insert_n(...)
vector< " " >::insert(...)
vector<" ">::push_back(...)
It looks like maybe the vector isn't valid, because _MyFirst and similar data members are showing values of 0xcdcdcdcd in the debugger. But the vector is a member variable...
Update: The vector isn't valid because it's never made. I'm getting a channel ID value stomp, which is making me treat one type of channel as another.
Update:
Searching through with the debugger again, it appears that my method for giving each "channel" it's own, unique ID isn't giving me a unique ID:
inline static const char channel<template args>::idFunction() {
return reinterpret_cast<char>(&channel<CHANNEL_IDENTIFY>::idFunction);
};
Update2: These two are giving the same:
slaveChannel<switchboard, ALLEGRO_BITMAP*, entityInfo<ALLEGRO_BITMAP*>
slaveChannel<key<c>, char, push<char>
Sooo, having another compiled channel type changing things makes sense, because it shifts around the values of the idFunctions? But why are there two idFunctions with the same value?
you seem to be returning address of the function as a character? that looks weird. char has much smaller bit count than pointer, so it's highly possible you get same values. that could reason why changing code layout fixes/breaks your program
As a general answer (though aaa's comment alludes to this): When something like this affects whether a bug occurs, it's either because (a) you're wrong and it is being run, or (b) the way that the inclusion of that code happens to affect your code, data, and memory layout in the compiled program causes a heisenbug to change from visible to hidden.
The latter generally occurs when something involves undefined behavior. Sometimes a bogus pointer value will cause you to stomp on a bit of your code (which might or might not be important depending on the code layout), or sometimes a bogus write will stomp on a value in your data stack that might or might not be a pointer that's used later, or so forth.
As a simple example, supposing you have a stack that looks like:
float data[10];
int never_used;
int *important pointer;
And then you erroneously write
data[10] = 0;
Then, assuming that stack got allocated in linear order, you'll stomp on never_used, and the bug will be harmless. However, if you remove never_used (or change something so the compiler knows it can remove it for you -- maybe you remove a never-called function call that would use it), then it will stomp on important_pointer instead, and you'll now get a segfault when you dereference it.

accessing element of boost sparse_matrix seems to stall program

I've got a strange bug that I'm hoping a more experience programmer might have some insight into. I'm using the boost ublas sparse matrices, specifically mapped_matrix, and there is an intermittent bug that occurs eventually, but not in the initial phases of the program. This is a large program, so I cannot post all the code but the core idea is that I call a function which belongs to a particular class:
bool MyClass::get_cell(unsigned int i, unsigned int j) const
{
return c(i,j);
}
The variable c is defined as a member of the class
boost::numeric::ublas::mapped_matrix<bool> c;
When the bug occurs, the program seems to stop (but does not crash). Debugging with Eclipse, I can see that the program enters the boost mapped_matrix code and continues several levels down into std::map, std::_Rb_tree, and std::less. Also, the program occasionally traces down to std::map, std::_Rb_tree, and std::_Select1st. While code is executing and the active line what's in memory changes in _Rb_tree, execution never seems to return in the level of std::map. The line in std::map the program is stuck on is the return of the following function.
const_iterator
find(const key_type& __x) const
{ return _M_t.find(__x); }
It seems to me that there is some element in the c matrix that the program is looking for but somehow the underlying storage mechanism has "misplaced it". However, I'm not sure why or how to fix it. That could also be completely off base.
Any help you can provide would be greatly appreciated. If I have not included the right information in this question, please let me know what I'm missing. Thank you.
Some things to try to debug the code (not necessarily permanent changes):
Change the bool to an int in the matrix type for c, to see if the matrix expects numeric types.
Change the matrix type to another with a similar interface, possibly plain old matrix.
Valgrind the app (if you're on linux) to check you're not corrupting memory.
If that fails, you could try calling get_cell every time you modify the matrix to see what might be causing the problem.
Failing that, you may have to try reduce the problem to a much smaller subset of code which you can post here.
It might help if you tell us what compiler and OS you're using.
Is this part of a multithreaded program?
I ask, because usually when I see problems in STL, it ends up being a problem with unsynchronized access.