class - C++ unexpected change of members data -


one of class has trouble: members data can change unexpectedly.

i have read similar subject on so, , seems apparent undefined behavior or problem of pointer. easy expression of code, still have it:

aid.cpp:

#include "aid.h"  bool aid::detect(t_arr3d x, t_arr3d x_p1, t_arr3d x_p2, t_arr3d x_p3, t_arr3d x_p4, int fp) {     return false; }  aid::aid() {     this->counter = 0;     maxerrorbound = 0.1;     maxerrorbound2 = 0.02; // = maxerrorbound * lambda } 

aid.h

#ifndef aid_h_ #define aid_h_  #include "detector.h" #include "vec.h" #include <map> #include <vector> #include <limits> #include <cstddef> #include <boost/assign/list_of.hpp> #include <boost/unordered_map.hpp> #include "constants.h" using namespace rode; using boost::assign::map_list_of; using namespace std;   class aid: public detector {  public:     bool detect(t_arr3d x, t_arr3d x_p1, t_arr3d x_p2, t_arr3d x_p3, t_arr3d x_p4, int fp);      aid();  private:     int counter;     float maxerrorbound  ;     float maxerrorbound2;  }; #endif 

the class called in 1 (rode.cpp):

... if(a_detector == "aid"){     aid d = aid( );     this->aid = &d; }  ... 

with lldb, have put watchpoint check going on:

watchpoint 1 hit: old value: 0 new value: 1606405696 process 38408 stopped * thread #1: tid = 0x74cd2, 0x00007fff5fc12171 dyld`imageloadermacho::findexportedsymbol(char const*, bool, imageloader const**) const + 13, queue = 'com.apple.main-thread', stop reason = watchpoint 1     frame #0: 0x00007fff5fc12171 dyld`imageloadermacho::findexportedsymbol(char const*, bool, imageloader const**) const + 13 dyld`imageloadermacho::findexportedsymbol: ->  0x7fff5fc12171 <+13>: pushq  %rax     0x7fff5fc12172 <+14>: movq   %rcx, %r14     0x7fff5fc12175 <+17>: movl   %edx, -0x2c(%rbp)     0x7fff5fc12178 <+20>: movq   %rsi, %r15 (lldb) bt * thread #1: tid = 0x74cd2, 0x00007fff5fc12171 dyld`imageloadermacho::findexportedsymbol(char const*, bool, imageloader const**) const + 13, queue = 'com.apple.main-thread', stop reason = watchpoint 1   * frame #0: 0x00007fff5fc12171 dyld`imageloadermacho::findexportedsymbol(char const*, bool, imageloader const**) const + 13     frame #1: 0x00007fff5fc184f6 dyld`imageloadermachocompressed::resolvetwolevel(imageloader::linkcontext const&, imageloader const*, bool, char const*, bool, imageloader const**) + 86     frame #2: 0x00007fff5fc18784 dyld`imageloadermachocompressed::resolve(imageloader::linkcontext const&, char const*, unsigned char, long, imageloader const**, imageloadermachocompressed::lastlookup*, bool) + 276     frame #3: 0x00007fff5fc1a09b dyld`imageloadermachocompressed::dobindfastlazysymbol(unsigned int, imageloader::linkcontext const&, void (*)(), void (*)()) + 235     frame #4: 0x00007fff5fc0424e dyld`dyld::fastbindlazysymbol(imageloader**, unsigned long) + 90     frame #5: 0x00007fff9610b3ba libdyld.dylib`dyld_stub_binder + 282     frame #6: 0x000000010004f268 wrf2sl`gcc_except_table678 + 3660     frame #7: 0x00000001000270f9 wrf2sl`main(argc=15, argv=0x00007fff5fbffaa0) + 21337 @ wrf2sl.cc:170     frame #8: 0x00007fff9610d5c9 libdyld.dylib`start + 1 

wrf2sl program. rest not related it.

have ever seen similar trouble? how should check understand going on?

the problem here

if(a_detector == "aid"){     aid d = aid( );     this->aid = &d; }  

here create local variable in scope of if body, , it's local inside there. store pointer local variable, pointer object destructed once if statement done. lead undefined behavior when try dereference pointer non-existent object.

my advice not use pointers start with, , instead store object value (i.e. actual instance of aid class). if must use pointers, allocate dynamically new, , remember delete when you're done (or optionally depending on use-case use smart pointer).


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -