Picture of iqbal
Registered 5 years 82 days
iqbal Monday, 2 January 2023, 10:25 AM
C++ App crashes regularly with Segmentation fault.
Hi
we have a C++ App which crashes regularly.

a Snippet of stack trace output is following:


01-01 23:02:41.690 dumping stack trace from core dump /var/log/core_dumps/LineSelect/LineSelect.core.dump (current build:130005)

[New LWP 10185]
[New LWP 10192]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `/apps/LineSelect/LineSelect --manager=/var/run/manager/manager --service-id=lin'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000000000479afe in PostgreSQLDatabase::ReadResult() ()
[Current thread is 1 (Thread 0x7f0797d3d100 (LWP 10185))]

Thread 2 (Thread 0x7f0797d39700 (LWP 10192)):
#0 0x00007f07987ea77c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00000000004c5603 in DnsThread::ThreadFunction() ()
#2 0x00000000004c481a in __dns_thread_main(void*) ()
#3 0x00007f07987e3f3d in start_thread () from /lib64/libpthread.so.0
#4 0x00007f0797e7a9af in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x7f0797d3d100 (LWP 10185)):
#0 0x0000000000479afe in PostgreSQLDatabase::ReadResult() ()
#1 0x000000000047a3b0 in non-virtual thunk to PostgreSQLDatabase::IoContextNotify(bool, bool) ()
#2 0x000000000045e597 in IoMux::Run() ()
#3 0x0000000000409742 in main ()

I can send complete Logoutput if needed.
Please tell me how can I Fix these typ of errors.
Best Regards
Iqbal


Picture of Daniel Deterding (innovaphone)
Moderator Registered 14 years 347 days
Daniel Deterding (innovaphone) Monday, 2 January 2023, 10:38 AM
Re: C++ App crashes regularly with Segmentation fault.
Hi Iqbal,

it crashes in PostgreSQLDatabase::ReadResult.
This function itself calls the DatabaseExecSQLResult of one of your classes.

So I assume, that this class has been deleted before it retrieved the last outstanding DatabaseExecSQLResult callback.

Greetings,
Daniel
Picture of iqbal
Registered 5 years 82 days
iqbal Monday, 2 January 2023, 12:11 PM
Re: C++ App crashes regularly with Segmentation fault.
Hi Daniel
Then how can I prevent that this happens. Actually I am still confused with Asyncronous Task behavior and how to implement it correctly. Code Demos on your site are not very helpfull, they only explain every thing in short but not whole implementation of all components working together.
I still have some questions about this.
1. how can I implement Task chaining in clean and simple manner?
2. how can I handle task manualy and how do i prevent class destruction before task is completed?
3. how can I use Tasks in loops.
4. is there a way to use "asyc await" functions of c++ in SDK instead of tasks or will be possible in future usage of this?

Best Regards
Iqbal
Picture of iqbal
Registered 5 years 82 days
iqbal Friday, 6 January 2023, 04:37 PM in response to Daniel Deterding (innovaphone)
Re: C++ App crashes regularly with Segmentation fault.
Hi
can anyone help about this situation. Or do you have some example code where I can see how to avoid these errors.
Best Regards
Iqbal

Picture of Daniel Deterding (innovaphone)
Moderator Registered 14 years 347 days
Daniel Deterding (innovaphone) Monday, 9 January 2023, 07:48 AM
Re: C++ App crashes regularly with Segmentation fault.
Hi Iqbal,

there is no best way how to handle tasks. It depends on what tasks are doing and your subjective coding style.

But there are two simple rules:
never delete a task/object which is asynchronously waiting for a callback and always hold a reference to tasks/objects which do async stuff.

A simple example:
- you create an object for an appwebsocket connection (usually your AppSession class)
- you get a message type "FetchDatabaseObjects" and you create a task for this
- the task is a UDatabase object and implements DatabaseError and DatabaseExecSQLResult
- in the Start function of this task you call database->ExecSql ... so there will be either a DatabaseError callback or a DatabaseExecSQLResult callback
- before this callback now triggers, your session class may get an AppWebsocketClosed callback
- now the session class must know that it started a task and that this task is not finished yet, thus our example App holds a currentTask pointer which is checked inside TryClose
- if the task is not yet finished, the session class won't get deleted and so the task object itself also won't get deleted (the destructor doesn't contain a delete currentTask anyway)

And this is just one of many scenarios where you have to take care.

Greetings,
Daniel
Andreas Fink
Moderator Registered 12 years 278 days
Andreas Fink (innovaphone) Monday, 9 January 2023, 02:40 PM in response to iqbal
Re: C++ App crashes regularly with Segmentation fault.
Hello Iqbal,

you can also add log output into the class constructor and destructor, so you can see which one is called too early, before the segmentation fault happens.

Best Regards
Andreas Fink
← You can define your color theme preference here