-2

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

  • 2
    You need to provide a working (or rather, failing, and preferably minimal, but complete) example. – Cheers and hth. - Alf Dec 19 '14 at 19:04
  • the problem is not in the code you showed. you need to give us a minimal, complete and verifiable example. – zoska Dec 19 '14 at 19:04
  • A complete code of both the calling function and the receiving function will be good enough? – LifePhilPsyPro Dec 19 '14 at 19:09
  • A working sample. Code that we can compile and run. – Félix Cantournet Dec 19 '14 at 19:30
  • Ok so here is a code u can compile. http://pastebin.com/iLBxhtT0 its not the complete thing, if u wont find a thing here it means the problem is with the different files... – LifePhilPsyPro Dec 19 '14 at 19:45
  • @LifePhilPsyPro Have a thourough look at my answer, and read the links I have provided. Hitting UB, really is the most probable reason for different behavior in debug or release mode. Findinng the possible points where UB occurs, you'll need a memory or execution profiling tool as I have mentioned. – πάντα ῥεῖ Dec 19 '14 at 21:13
  • @πάνταῥεῖ sadly i run windows currently and as i understand valgrind currently doesnt run on that platform :( – LifePhilPsyPro Dec 19 '14 at 21:39
  • 1
    `std::vector` is suspect. `point` is simple enough that you want to avoid the overhead of dynamic memory allocation, plus the fact that you don't want to have to manually delete all the members. Just use `std::vector`. – Peter Dec 19 '14 at 21:44
  • @LifePhilPsyPro Here are some viable alternatives mentioned: [Is there a good Valgrind substitute for Windows?](http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows). Also there's such feature for VS2013 intrinsically available IIRC. Nevertheless, your problems seem to be raised from different code, as you're actually showing here. – πάντα ῥεῖ Dec 19 '14 at 21:46
  • @Peter Wow great. That worked... but why?! Could you post some explanation for others to see as well what might go wrong here? Ill mark it as the chosen answer to see it quickly – LifePhilPsyPro Dec 19 '14 at 21:49
  • 1
    Just to clarify: my comment probably does not identify your bug. It's a suggestion for simplifying and improving the code. If you make this change (and maybe remove some other uses of pointers and new, converting them to simple stack variables and pass-by-reference), you'll reduce the opportunities for mistakes with pointers. – Peter Dec 19 '14 at 21:53
  • That may be true. But question is what can possibly go wrong with a simplified case like that? Isn't there a simple way to determine what the problem is someway easily? or at least is there a design pattern or some style guideline to prevent such things? – LifePhilPsyPro Dec 19 '14 at 21:55
  • Converting to answer... – Peter Dec 19 '14 at 21:57

2 Answers2

2

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.

Peter
  • 14,559
  • 35
  • 55
0

"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.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    Why is this voted-1 ? This is a good diagnostic for "Works in debug, doesn't work in release". – Félix Cantournet Dec 19 '14 at 19:29
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – user35443 Dec 19 '14 at 19:31
  • @user35443 Given the narrow context of the OP's post I think that well constitutes an answer, and points out for the essentials to solve the OP's problem. Please even if you're working on the queue, read the answer and the implications thouroughly. – πάντα ῥεῖ Dec 19 '14 at 19:44